0

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

Here is my code.

Objective C Code

@implementation arrayTest
{
    NSMutableArray *mutableArray;
}
- (id) init {
    self = [super init];
    if(self) {
        mutableArray = [[NSMutableArray alloc] init];
    }
    return self;
}
- (NSMutableArray *) getArray {
    for(...; ...; ...) {
          ...
        NSString *cutFinal = xxx;
        [mutableArray addObject: cutFinal];
    }
    return mutableArray; // mutableArray = {2, 5, 10}
}

Swift Code

var target = arrayTest.getArray() // target = {2, 5, 10} 

for index in 1...10 {
    for targetIndex in 0..target.count {
        if index == target.objectAtIndex(targetIndex) 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


I solved the problem !!!

my Objective C code

NSString *cutFinal = xxx;
[mutableArray addObject: cutFinal];

fix

NSString *cutFinal = xxx;
NSNumber *add = [NSNumber numberWithInteger:[cutFinal intValue]];
[mutableArray addObject: add];

and my Swift code

 var target = arrayTest.getArray() as [Int]
SaeHyun Kim
  • 475
  • 2
  • 8
  • 26
  • You could just use subscripts rather than `objectAtIndex` (in Objective-C *or* Swift), but your loop logic is wrong, and you will not get what you expect. – Grimxn Jul 14 '14 at 07:49
  • you may want to cast as `NSString`, regarding that is the type which you used populating the array before... – holex Jul 14 '14 at 08:42
  • Can you help me at all: http://stackoverflow.com/questions/28042105/swift-dynamic-cast-failed-swift-dynamiccastclassunconditional – Nate Uni Jan 21 '15 at 03:51

1 Answers1

0

I would have taken the best out of Swift actually, writing something like this instead:

    for index in 1...10 {
        let array = target.filter{$0 == index}
        let value = array.count == 0 ? "\(index)" : "GET"
        println("\(value)")
    }
DennyLou
  • 313
  • 2
  • 4