如何迅速
* IDE使用objectAtIndex:XCODE 6 beta3版
*语言:斯威夫特+目标C如何迅速
这里是我的代码。
目标C代码
@implementation arrayTest
{
NSMutableArray *mutableArray;
}
- (id) init {
self = [super init];
if(self) {
mutableArray = [[NSMutableArray alloc] init];
}
return self;
}
- (NSMutableArray *) getArray {
...
return mutableArray; // mutableArray = {2, 5, 10}
}
Swift代码
var target = arrayTest.getArray() // target = {2, 5, 10}
for index in 1...10 {
for targetIndex in 1...target.count { // target.count = 3
if index == target.objectAtIndex(targetIndex-1) as Int {
println("GET")
} else {
println(index)
}
}
}
我想以下结果:
1 GET 3 4 GET 6 7 8 9 GET
但是,我的代码给我的错误
libswift_stdlib_core.dylib`swift_dynamicCastObjCClassUnconditional:
0x107e385b0: pushq %rbp
...(skip)
0x107e385e4: leaq 0xa167(%rip), %rax ; "Swift dynamic cast failed"
0x107e385eb: movq %rax, 0x6e9de(%rip) ; gCRAnnotations + 8
0x107e385f2: int3
0x107e385f3: nopw %cs:(%rax,%rax)
。
if index == target.objectAtIndex(targetIndex-1) as Int {
// target.objectAtIndex(0) = 2 -> but type is not integer
我觉得这段代码是不完整的。 但我找不到解决方案。
帮我TT
在的OBJ-C,objectAtIndex:2成这个样子的:
[self.myArray ObjectAtIndex:2]
在斯威夫特objectAtIndex:2成这个样子的:
self.myArray[2]
我一直在使用模拟你的数组:
NSArray * someArray() {
return @[@2, @5, @10];
}
而且你的代码编译并没有问题上运行的Xcode 6 Beta 3的
但是,你的代码没有做你想要什么,因为它打印10 * target.count
号码
正确的,它应该是
let target = arrayTest.getArray() as [Int]
for index in 1...10 {
var found = false
for targetIndex in indices(target) {
if index == target[targetIndex] {
found = true
break
}
}
if (found) {
println("GET")
} else {
println(index)
}
}
甚至更好
let target = arrayTest.getArray() as [Int]
for index in 1...10 {
if (contains(target, index)) {
println("GET")
} else {
println(index)
}
}
1. let target = arrayTest.getArray()as [Int] - >'AnyObject'与'Int'不相同 2. if(contains(target,index)){ - >'NSNumber'不是'S.GeneratorType.Element - > L' –
@SaeHyunKim您确定您使用的是最新版本? Beta 3? – Sulthan
我的xcode版本是'版本6.0(6A254o)'(测试版3) –
“ Swift动态转换失败“你的数组不包含'Int',请尝试打印数组 –
它可能包含'NSNumber' ins tances。 – Sulthan