ios 如何快速使用 objectAtIndex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24696913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:20:28  来源:igfitidea点击:

How to use objectAtIndex in swift

iosobjective-cswift

提问by SaeHyun Kim

*IDE: XCODE 6 beta3
*Language: Swift + Objective C

*IDE:XCODE 6 beta3
*语言:Swift + Objective C

Here is my code.

这是我的代码。

Objective C Code

目标 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 Code

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)
        }
    }
}

I want the following result:

我想要以下结果:

1 GET 3 4 GET 6 7 8 9 GET

But, my code gives me the error

但是,我的代码给了我错误

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

I think this code is incomplete. But I can't find the solution.
Help me T T

我认为这段代码不完整。但我找不到解决方案。
帮帮我TT

回答by iOSAppGuy

In Obj-C, objectAtIndex: 2 looks like this:

在 Obj-C 中, objectAtIndex: 2 看起来像这样:

[self.myArray ObjectAtIndex:2]

In Swift objectAtIndex: 2 looks like this:

在 Swift objectAtIndex: 2 中看起来像这样:

self.myArray[2]

回答by Sulthan

I have simulated your array using:

我已经使用以下方法模拟了您的数组:

NSArray * someArray() {
    return @[@2, @5, @10];
}

And your code compiles and runs without problems in Xcode 6 Beta 3

并且您的代码在Xcode 6 Beta 3 中编译和运行都没有问题

However, your code doesn't do what you want because it prints 10 * target.countnumbers

但是,您的代码不会执行您想要的操作,因为它会打印10 * target.count数字

Correctly, it should be

正确的应该是

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)
    }
}

or even better

甚至更好

let target = arrayTest.getArray() as [Int]

for index in 1...10 {
    if (contains(target, index)) {
        println("GET")
    } else {
        println(index)
    }
}