Cha*_*eon 2 core-data nsmutablearray ios nsnull swift
我有一个PersonsArray: NSMutableArray = [NSNull, NSNull, NSNUll, NSNull, NSNull, NSNUll, NSNull].我需要七个插槽,然后我可以用EntObject CoreData条目填充AnyObject.
我需要在这个NSMutableArray上循环执行...
如果索引槽是NSNull我想传递给下一个索引槽,如果索引槽填充了我的对象我想在这个对象上执行代码.
example PersonsArray: NSMutableArray = [
NSNull,
NSNull,
NSNull,
"<iswift.Person: 0x7f93d95d6ce0> (entity: Person; id: 0xd000000000080000 <x-coredata://8DD0B78C-C624-4808-9231-1CB419EF8B50/Person/p2> ; data: {\n image = nil;\n name = dustin;\n})",
NSNull,
NSNull,
NSNull
]
Run Code Online (Sandbox Code Playgroud)
尝试
for index in 0..<PersonsArray.count {
if PersonsArray[index] != NSNull {println(index)}
}
Run Code Online (Sandbox Code Playgroud)
提出了一系列不起作用的变化,比如
if PersonsArray[index] as! NSNull != NSNull.self {println(index)}
Run Code Online (Sandbox Code Playgroud)
要么
if PersonsArray[index] as! NSNull != NSNull() {println(index)}
Run Code Online (Sandbox Code Playgroud)
注意:使用NSNull只是NSMutableArray中的占位符,因此它的计数总是7,我可以用Object替换任何(7)槽.我应该使用NSNull以外的东西作为我的占位符吗?
NSNull()是一个单例对象,因此您可以简单地测试数组元素是否是以下实例NSNull:
if personsArray[index] is NSNull { ... }
Run Code Online (Sandbox Code Playgroud)
或使用"相同"运算符:
if personsArray[index] === NSNull() { ... }
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用一组可选项:
let personsArray = [Person?](count: 7, repeatedValue: nil)
// or more verbosely:
let personsArray : [Person?] = [ nil, nil, nil, nil, nil, nil, nil ]
Run Code Online (Sandbox Code Playgroud)
使用nil对空槽.
| 归档时间: |
|
| 查看次数: |
5427 次 |
| 最近记录: |