在一个swift文件中创建多个类被认为是不好的技术,还是最好swift为每个类创建一个单独的文件?
例如,哪个更好:
1. alpha.swift:
class alpha: UIViewController { ... }
class beta: UIWebView { ... }
class gamma: UINavigationController { ... }
Run Code Online (Sandbox Code Playgroud)
1.在alpha.swift中:
class alpha: UIViewController { ... }
Run Code Online (Sandbox Code Playgroud)
2.在beta.swift中:
class beta: UIWebView { ... }
Run Code Online (Sandbox Code Playgroud)
3.在gamma.swift中:
class gamma: UINavigationController { ... }
Run Code Online (Sandbox Code Playgroud) 我正在使用UIWebView构建自定义浏览器.
使用案例:用户在地址栏中输入"www.abc.com".抛出以下错误:
Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0x19860770 {NSErrorFailingURLKey=file://www.abc.com, NSErrorFailingURLStringKey=file://www.abc.com, NSLocalizedDescription=Frame load interrupted}
原因:网址需要添加"http://"
我想使用stringWithFormatNSString 的方法,但我似乎无法得到正确的语法.在Objective-C中,我们有;
NSString* modifiedURLString = [NSString stringWithFormat:@"http://%@", urlString];
Run Code Online (Sandbox Code Playgroud)
在Swift中,方法不存在?!
var modifiedURLString: String = String(`stringWithFormat not here?!...`)
Run Code Online (Sandbox Code Playgroud)
然后我尝试将Objective C与Swift混合:
var modifiedURLString:NSString = [NSString stringWithFormat not here?!...
然后我尝试了直接的Objective-C:
NSString*modifiedURLString = [NSString stringWithFormat:@"http://",urlString];
谢谢您的帮助.真诚的,基思
我按如下方式设置了一个观察者,其中包括以下logYes()功能:
class SplashPageVC: UIViewController {
func logYes() {
println("Yes");
}
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "logYes:", name: "userValid", object: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
我已经将以下IBAction连接到一个按钮:
class LoginVC: UIViewController {
@IBAction func loginSubmitted(sender : AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("userValid", object: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
点击按钮时出现以下错误:
[_TtC13Explorer12SplashPageVC self.logYes:]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)
我尝试了一堆不同的选择器,没有运气:
logYes
logYes:
logYes()
logYes():
Run Code Online (Sandbox Code Playgroud)
我没有想法.任何见解?tyvm :)
参考文献:
在postNotificationName中没有发送
NSNotification :在Swift 中的swift委托中称为NSNotificationCenter addObserver
?
我试图连接字符串和整数,并使用登录到控制台println.
println("Load number: " + webViewLoads)
Run Code Online (Sandbox Code Playgroud)
webViewLoads是'Int'类型.由于我在这里混合了两种类型,因此我收到错误并不奇怪:
Could not find an overload for 'println' that accepts the supplied arguments.
所以,我尝试将webViewLoads转换as为字符串:println("Load:"+ webViewLoads as String)
Grr ..错误仍然被抛出.
我怎样才能使这个简单的小连接起作用?
我正在尝试设置一个全局变量.在我的例子中,只是一个布尔标志,指示是否第一次呈现视图:
var initialLoadFlag: Bool = true
Run Code Online (Sandbox Code Playgroud)
在显示视图后,我想将此标志设置为false:
var initialLoadFlag: Bool = false
Run Code Online (Sandbox Code Playgroud)
然后检查它:
if initialLoadFlag {
showWelcomeMessage()
}
Run Code Online (Sandbox Code Playgroud)
所以,我想创建initialLoadFlag一个全局变量.在哪里以及如何?我试过了:
viewDidLoad我的视图控制器区域application()我的AppDelegate.swift文件中的方法中AppDelegate课堂上没运气.我收到一条Use of unresolved identifier 'initialLoadFlag'错误消息
(注意:我意识到在这个问题中,我背叛了我对Swift如何处理范围的无知.请原谅我......我正处于截止日期,并且仍然对语言不熟悉.)
谢谢你的帮助.
我正在学习Visual Studio 2015社区.我是一名经验丰富的程序员,但VS的新手,文件表示让我感到困惑.我已经创建了一个解决方案,并添加了一个现有项目.VS正显示出我所有项目文件(在我的情况下,C++项目,所以.c,.cpp和.h文件)Solution Explorer中一个树级别.
相反,如果我用Sublime Text,VS Code,notepad ++等开放所述项目,我会看到正确的目录结构,因为它位于磁盘驱动器上; 就像人们会在文件资源管理器/ Finder或ls/ dir终端中看到它一样.
我在发布之前完成了我的作业,在接下来的帖子中,@安德里说:
恐怕在MSVS中没有像"目录结构"这样的概念.此外,MSVS并不真正需要它,因为它使用平面项目,层次结构基于项目级别,而不是文件/目录级别.
因为没有这样的东西 - 你既不能自动也不能手动.MSVS具有解决方案文件夹,这是完全不同的事情,并且将真实文件夹表示为解决方案文件夹没有多大意义.
Visual Studio作为代码浏览器:如何保留目录结构?
这是真的?文件的排列有很多意义,VS中的平面表示使项目难以理解; 目录结构中的文件位置很重要.有没有办法在VS中查看正确的目录结构?
在我的情况下,我正在使用Quickfix,它支持多个版本(4.0,4.1,4.2等).其中每个都有不同的类和具有相同名称的文件.
正如您从下面的屏幕截图中看到的那样,它们都整齐地排列在磁盘上的不同文件夹中,但VS对这些文件的表示非常混乱:
directory-structure solution visual-studio visual-studio-2015
(Xcode6,iOS8,iPhone,Swift)
我想在导航栏的右侧添加一个"继续"按钮.
如何实现这一目标?我一直在尝试使用UIBarButtonItem上的一些方法,但无法使其正常工作.
我迄今为止的最大努力是:
var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStyle, target: self, action: nil)
self.navigationItem.rightBarButtonItem = b
Run Code Online (Sandbox Code Playgroud)
但我在第一行遇到错误.它不喜欢"style"参数.我也试过了
var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStylePlain, target: self, action: nil)
Run Code Online (Sandbox Code Playgroud)
但没有运气.仍然停留在样式参数上.有任何想法吗?tyvm Keith:D
编辑:对于后代,以下行还包括一个action设置:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello")
Run Code Online (Sandbox Code Playgroud)
(Xcode 6.0.1,iOS 7.1.1,iPad2)
尝试将图像设置为按钮背景.
图像显示在模拟器中,但不在设备上显示.这让我相信它们并没有与应用程序捆绑在一起,尽管我在构建中包含了images.xcassets.
我想检查并查看是否包含三个新图像集,但似乎没有办法在"构建设置"下的"复制捆绑资源"分支中深入查看images.xcassets.
我通过单击图像视图,选择"属性检查器"按钮,然后在"背景"下的下拉列表中选择图像名称,在故事板中设置图像背景.
其他图像工作; 它只是新添加的图像没有显示出来.
我很难过......有什么建议吗?
使用complex类和库,如何为变量分配复数?
我知道我可以在第一次实例化复数时设置该值.
我也明白我可以将一个实例化的复数分配给另一个.
如何直接为变量分配复数?
参考:http:
//www.cplusplus.com/reference/complex/complex/operators/
例:
#include <iostream>
#include <complex>
int main() {
complex<double> a(1.2,3.4), b;
cout << a; //-> (1.2,3.4)
b = a;
cout << b; //-> (1.2,3.4)
b = (1.2,3.4);
cout << b; //-> (3.4,0) <-- what the heck is this??
return 0;
}
Run Code Online (Sandbox Code Playgroud) 当构造函数抛出异常时,如何防止创建对象?
在下面的示例中,我创建了一个Month()类,其int month_属性的合法值在1到12的范围内.I实例化12月,或者 dec,使用整数值13.抛出异常,因为它应该是,但仍然创建了该对象.然后调用析构函数.
如何在抛出异常时中止创建类实例?
OUTPUT
-- Month() constructor called for value: 2
-- Month() constructor called for value: 6
-- Month() constructor called for value: 13
EXCEPTION: Month out of range
2
6
13
-- ~Month() destructor called.
-- ~Month() destructor called.
-- ~Month() destructor called.
Press any key to exit
Run Code Online (Sandbox Code Playgroud)
最小,完整和可验证的例子
#include <iostream>
#include <string>
class Month {
public:
Month(int month) {
std::cout << "-- Month() constructor called for value: " << month …Run Code Online (Sandbox Code Playgroud)