ios - 如何在 swift 中使用 objectAtIndex

标签 ios objective-c swift

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

这是我的代码。

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

快速代码

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

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

最佳答案

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

[self.myArray ObjectAtIndex:2]

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

self.myArray[2]

关于ios - 如何在 swift 中使用 objectAtIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24696913/

相关文章:

iphone - 将文件上传到 Google 云端硬盘时出错

ios - 从 UserDefualts 设置 UILabel

jquery - 此代码在 iOS 9.3 后在 Safari 中停止工作

ios - 显示黑屏而不是 UICollectionView

iphone - 将十六进制转换为 NSData

ios - 计算MBProgressHUD下载进度

ios - 在应用程序启动时呈现不同的 UIViewController

ios - 自动布局以保持 View 大小成比例

iphone - 我怎样才能避免 AlAssetLibrary 中的位置服务?我可以在不使用定位服务的情况下使用 AlAssetLibrary 检索文件吗?

ios - 关闭应用程序时如何在后台运行快速应用程序?