我的Swift程序崩溃了EXC_BAD_INSTRUCTION
,这个错误.它是什么意思,我该如何解决?
致命错误:在展开Optional值时意外发现nil
这篇文章旨在收集"意外发现的零"问题的答案,因此它们并不分散且难以找到.随意添加自己的答案或编辑现有的维基答案.
在Apple关于与C API交互的文档中,他们描述了NS_ENUM
以Swift枚举方式导入标记的C风格枚举的方式.这是有道理的,因为Swift中的枚举很容易作为enum
值类型提供,所以很容易看到如何创建自己的.
更进一步,它说这是关于NS_OPTIONS
标记的C风格选项:
Swift还导入标有
NS_OPTIONS
宏的选项.而选项的行为类似于进口枚举,选项还可以支持一些位操作,如&
,|
和~
.在Objective-C中,表示使用常量零(0
)设置的空选项.在Swift中,用于nil
表示没有任何选项.
鉴于options
Swift中没有值类型,我们如何创建一个C-Style选项变量来使用?
我有三个UIBarButtonItem
创建如下.它们左对齐,我想对齐中心,所以右侧没有间隙.我没有看到对齐属性UIToolBar
.还有另一种方法来实现这一目标吗?
//create some buttons
UIBarButtonItem *aboutButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(showAbout:)];
[toolbar setItems:[NSArray arrayWithObjects:settingsButton,deleteButton,aboutButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
Run Code Online (Sandbox Code Playgroud) 对于以下示例:
def fuctionName(int, bool):
if int in range(...):
if bool == True:
return False
else:
return True
Run Code Online (Sandbox Code Playgroud)
有没有办法跳过第二个if语句?只是告诉计算机返回布尔值的反面bool
?
如果x
是double
,则(x - x)
保证是+0.0
,也可能有时-0.0
(取决于标志x
)?
在回答这个问题时,我们发现了一个调用标签是必需的init
.这在Swift中是正常的.
class Foo {
init(one: Int, two: String) { }
}
let foo = Foo(42, "Hello world") // Missing argument labels 'one:two:' in call
Run Code Online (Sandbox Code Playgroud)
然而,陌生人的力量在起作用:
extension Foo {
func run(one: String, two: [Int]) { }
}
foo.run(one: "Goodbye", two: []) // Extraneous argument label 'one:' in call
Run Code Online (Sandbox Code Playgroud)
要在此处使用参数标签,必须明确声明.
我没有在文档中看到非常详尽的解释所有这些内容.哪些类/实例/全局函数是必需的参数标签?是否始终使用参数标签导出和导入Obj-C方法?
我不小心写了这样的代码:
foo = [42]
k = {'c': 'd'}
for k['z'] in foo: # Huh??
print k
Run Code Online (Sandbox Code Playgroud)
但令我惊讶的是,这不是语法错误.相反,它打印{'c': 'd', 'z': 42}
.
我的猜测是代码翻译成字面意思:
i = iter(foo)
while True:
try:
k['z'] = i.next() # literally translated to assignment; modifies k!
print k
except StopIteration:
break
Run Code Online (Sandbox Code Playgroud)
但是......为什么语言允许这样做?我希望在for-stmt的目标表达式中只允许使用单个标识符和标识符元组.是否存在实际有用的情况,而不仅仅是一个奇怪的问题?
我有一个WebView,我想从JavaScript调用Objective-C中的一个视图.有人知道我怎么做吗?
我的ViewController中有这个代码:
- (BOOL)webView:(UIWebView *)webView2
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ([components count] > 1 &&
[(NSString *)[components objectAtIndex:0] isEqualToString:@"myapp"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"myfunction"])
{
NSLog([components objectAtIndex:2]); [[Airship shared] displayStoreFront]; //<- This is the code to open the Store
NSLog([components objectAtIndex:3]); // param2
// Call your method in Objective-C method using the above...
}
return NO;
}
return YES; // Return YES to make sure regular navigation works as …
Run Code Online (Sandbox Code Playgroud) 我刚刚下载了Xcode 6 beta 4,我的swift项目编译没有错误,但在它到达我的代码之前,我得到一个dyld_fatal_error就在调用堆栈的开头.
和带有nop指令的汇编代码中的断点
我得到的控制台错误是
dyld: lazy symbol binding failed: Symbol not found: __TFSsa6C_ARGVGVSs13UnsafePointerGS_VSs4Int8__
Referenced from: /Users/username/Library/Developer/Xcode/DerivedData/Sudoku-dhrdonaeqzsgcvewndimxbbsltnc/Build/Products/Debug/Sudoku.app/Contents/MacOS/Sudoku
Expected in: /Users/username/Library/Developer/Xcode/DerivedData/Sudoku-dhrdonaeqzsgcvewndimxbbsltnc/Build/Products/Debug/Sudoku.app/Contents/MacOS/../Frameworks/libswift_stdlib_core.dylib
dyld: Symbol not found: __TFSsa6C_ARGVGVSs13UnsafePointerGS_VSs4Int8__
Referenced from: /Users/username/Library/Developer/Xcode/DerivedData/Sudoku-dhrdonaeqzsgcvewndimxbbsltnc/Build/Products/Debug/Sudoku.app/Contents/MacOS/Sudoku
Expected in: /Users/username/Library/Developer/Xcode/DerivedData/Sudoku-dhrdonaeqzsgcvewndimxbbsltnc/Build/Products/Debug/Sudoku.app/Contents/MacOS/../Frameworks/libswift_stdlib_core.dylib
Run Code Online (Sandbox Code Playgroud)
只是因为你知道该项目仍在编译,并且在Xcode 6 beta 3中运行良好.
Swift提供is
关键字(和as?
)来检查对象是否可以成功向下转换:
if foo is MyClass {
// ...
}
Run Code Online (Sandbox Code Playgroud)
但NSObjectProtocol
也提供原始func isKindOfClass(aClass: AnyClass!) -> Bool
:
if something.isKindOfClass(MyClass) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
对于符合的类NSObjectProtocol
,这真的很不一样吗?