开发者

Problem with isEqualToString: method and NSInteger

This is my code:

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(in开发者_StackOverflow中文版t 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
        }
    }
}

When I try to NSLog("count %d",count); my app crash without any information about. I've also another problem with if([source isEqualToString:element.source]) I'm sure that some times the condition return true... How can I remove blank space? Like the trim function in php? thanks


Change:

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

to:

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

For removing blank spaces you can try:

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


I didn't notice the first two NSLog statements. They're both of the form NSLog("something: %@", something). That is a C string literal, whereas NSLog takes an NSString for its format. This will lead to a crash. You want NSLog(@"source: %@", source).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜