Pergunta

I have an app which works on 32-bit platforms but throws an error when running on 64-bit devices and simulators. The code in question is:

if ([lastStatus isEqualToString:@"0"]){ //EXC_BAD_ACCESS
                bRetVal = YES;
            }

From my class h file:

@interface DPXmlHandler : NSObject <NSXMLParserDelegate> {
    NSString *lastStatus;
    NSString *msg;
    NSMutableArray * locs;
    NSMutableArray *alerts;
    NSMutableArray *mrmMsgs;
    }
@property (nonatomic, copy) NSString *lastStatus;

I synthesize lastStatus at the top of my .m file. I am populating the string with a value from an XML file. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

if ([elementName isEqual:@"status"]) {
    //Get attribute value
    @try{
        lastStatus = [attributeDict valueForKey:@"code"];
        msg = [attributeDict valueForKey:@"message"];
    }@catch (NSException *e){
        NSLog(@"Exception: %@", e);
        msg = e.name;
    }

The value is an integer, but I'm failing to understand why I can't store a string of "0" and compare it to @"0". I would appreciate any insight on how to change this. The values for lastStatus are from roughly -100 to 100. I can make lastStatus an int or long and use the == operator, but I can't seem to find any guidance on WHY this is a problem.

I checked Apple's guide on converting apps to 64 bit, but there's no mention of NSString.

Foi útil?

Solução

Avoid direct use instance variables use properties instead.

if ([elementName isEqual:@"status"]) {
    //Get attribute value
    @try{
        self.lastStatus = [attributeDict valueForKey:@"code"];
        self.msg = [attributeDict valueForKey:@"message"];
    }@catch (NSException *e){
        NSLog(@"Exception: %@", e);
        self.msg = e.name;
    }

Because your's code doesn't retain or copy objects, they got autoreleased.

Outras dicas

ah, when you said you can make lastStatus and int or a long, it sounds like the value coming back for the key "code" is actually an int or a long type, and not an NSString type. the isEqualToString is a function of the NSString class, so if you are trying to compare it with a int or a long, it wont work. you can easily convert it by going @([attributeDict valueForKey:@"code"]).stringValue

note: @() is an NSNumber, @"" is an NSString

EDIT

sorry, you cant actually store ints or longs in a dictionary by themselves, since they need to be NSObject's to be able to be stored, so maybe they are already NSNumbers in there, then you can just use .stringValue on the object by itself

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top