我知道我可以找到我正在运行的Swift版本,现在恢复到终端并键入:
xcrun swift --version
Swift version 1.1 (swift-600.0.57.4)
Target: x86_64-apple-darwin13.4.0
Run Code Online (Sandbox Code Playgroud)
另外,我一直在阅读关于Swift中的预处理器宏,但没有运气找到Swift版本常量.
随着Swift 1.2的临近,标记仅在Swift 1.1(Xcode高达6.2)上运行的旧代码或需要Xcode 6.3的新代码(Swift 1.2)将会很不错
注意:我也可以使用system()来执行以下操作:
system("xcrun swift --version | grep version > somefile.txt")
Run Code Online (Sandbox Code Playgroud)
然后打开somefile.txt,但更喜欢一些更简单的解决方案
我有一个快速的协议:
@objc protocol SomeDelegate {
optional func myFunction()
}
Run Code Online (Sandbox Code Playgroud)
我做了我的一个课程:
weak var delegate: SomeDelegate?
Run Code Online (Sandbox Code Playgroud)
现在,我要检查是否delegate已经myFunction实现.
在objective-c中,我可以这样做:
if ([delegate respondsToSelector:@selector(myFunction)]) {
...
}
Run Code Online (Sandbox Code Playgroud)
但这在Swift中不可用.
编辑:这与以下内容不同:respontsToSelector的快速等价物是什么?我专注于不在类上的类协议.
如何检查我的委托是否已实施可选方法?
与StackOverflow上的许多其他问题类似,我unrecognized selector sent to instance在运行应用程序时遇到了错误.是什么让我的情况与众不同(我认为)是它用Swift编写的,并且常见的原因不是这里的原因.
在这个问题中,解决方案是将该视图的Identity Inspector中的"Custom Class"设置为响应类.我检查了这个情况(注意我在某个时候重命名了这个类,但它现在肯定设置为"ViewController").
我在下面包含堆栈跟踪以及代码ViewController.swift.您也可以在这里找到所有代码.我设法通过开始新鲜来避免这个问题,这使我能够解决我之前的问题.这个新的试验可以在这个分支中找到.但是,我想知道出了什么问题并解决了这个问题.
我无法成功回答的一个问题是,是否CLLocationManagerDelegate需要一些必要的方法?但是因为我让它在另一个分支机构工作,我怀疑它没有必要.
2014-07-03 21:50:26.056 RowingTracker2[11416:60b] -[CLLocationManager requestWhenInUseAuthorization]: unrecognized selector sent to instance 0xa10c4a0
2014-07-03 21:50:26.059 RowingTracker2[11416:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocationManager requestWhenInUseAuthorization]: unrecognized selector sent to instance 0xa10c4a0'
*** First throw call stack:
(
0 CoreFoundation 0x008831e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01e2a8e5 objc_exception_throw + 44
2 CoreFoundation 0x00920243 -[NSObject(NSObject) doesNotRecognizeSelector:] + …Run Code Online (Sandbox Code Playgroud) 是否可以检测是否定义了某些全局函数(非类方法)(在iOS中)?像respondsToSelector课堂上的东西......
我试图实现swift替代respondsToSelector:语法的替代方法,该语法也在主题演讲中展示.
我有以下内容:
protocol CustomItemTableViewCellDelegate {
func changeCount(sender: UITableViewCell, change: Int)
}
Run Code Online (Sandbox Code Playgroud)
然后在我调用的代码中
class CustomItemTableViewCell: UITableViewCell {
var delegate: CustomItemTableViewCellDelegate
...
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
...
delegate?.changeCount?(self, change: -1)
}
...
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Operand of postfix '?' should have optional type; type is
'(UITableViewCell, change:Int) -> ()'Operand of postfix '?' should
have optional type; type is 'CustomItemTableViewCellDelegate'Partial application of protocol method is not allowed我做错了什么?
谢谢