isEqualToString:method和NSInteger的问题

时间:2011-09-02 22:53:18

标签: iphone objective-c nsstring nsinteger

这是我的代码:

for (int i=0; i<countingArray.count; i++) {
    NSDictionary *element=[countingArray objectAtIndex:i];
    NSString *source=[element objectForKey:@"id"];
    NSInteger count= [[element objectForKey:@"count"] integerValue];
    NSLog("source: %@",source); //Works
    NSLog("count %d",count);    //Don't Work! Error at this line

    for(int c=0; c<subscriptions.count; c++) {
        SubscriptionArray * element =[subscriptions objectAtIndex:c];
        NSLog(@"sorgente sub %@",element.source);
        NSLog(@"sorgente counting %@",source);
        if([source isEqualToString:element.source]) {
            element.count=count;
            [subscriptions replaceObjectAtIndex:c withObject:element];
            NSLog(@"equal"); 
            //this part of code is never been executed but I'm sure that
            //the if condition in some cases returns true
        }
    }
}

当我尝试NSLog("count %d",count);我的应用程序崩溃时没有任何相关信息。 我还有if([source isEqualToString:element.source])的另一个问题我确信有时候条件会返回true ...如何删除空格?喜欢php中的trim功能?感谢

2 个答案:

答案 0 :(得分:1)

我没有注意到前两个NSLog语句。它们都是NSLog("something: %@", something)形式。这是一个C字符串文字,而NSLog为其格式采用NSString。这将导致崩溃。你想要NSLog(@"source: %@", source)

答案 1 :(得分:1)

变化:

NSString *source=[element objectForKey:@"id"];
NSInteger count= [[element objectForKey:@"count"] integerValue];

到:

 NSString *source=[element valueForKey:@"id"];
 NSInteger count= [[element valueForKey:@"count"] integerValue];

要删除空格,您可以尝试:

NSString *trimmedString = [yourString stringByReplacingOccurrencesOfString:@" " withString:@""];