开发者

Error while trying to print the structure variable-Objective C

-(BOOL)ChangeTimer:(unsigned short)wTimerIds withPeriod:(uint8_t)uPeriod
{
 stRs232Timer*   pEvent;
    NSLog(@"Into the changeTimer");
    NSLog(@"%d",wTimerIds);
    pEvent = (stRs232Timer*)[[m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]]bytes];
    NSLog(@"b开发者_开发知识库Persistent:%d",pEvent->bPersistent);
    NSLog(@"wAppTimerId:%d",pEvent->wAppTimerId);
    NSLog(@"uPeriod:%d",pEvent->uPeriod);
    NSLog(@"bStopped:%d",pEvent->bStopped);
    //NSLog(@"%@",pEvent);  
    if(pEvent!=nil){
        pEvent->bStopped = NO;
        pEvent->uPeriod = uPeriod;
        NSLog(@"completed");
    }   

    NSLog(@"The dict is:%@",m_cAppIdMap);

    NSLog(@"jsagxs:%@",m_cAppIdMap);
    return YES;
}

If i remove the comment in the above code i get an error EXC_BAD_ACCESS.Why is it so.


When you use the "%@" format specifier, you are telling the runtime to invoke the -descriptionWithLocale: or -description methods on the associated pointer whose class is implied to be a subclass of NSObject.

Structures are not Objective-C objects and therefore do not have a -descriptionWithLocale: or -description method. This is what is causing the EXC_BAD_ACCESS.

You should use %p for printing out pointers that don't point to Objective-C objects.


The format specifier %@ works by expecting an NSObject subclass, and calling descriptionWithLocale: or description (if descriptionWithLocale is absent) to obtain an NSString describing the object. stRs232Timer is just a C structure - not an Objective C object. When the runtime tries to treat it as an object things blow up, as you're seeing.

Try the %p specifier instead, which will just print the address of the item (i.e. the value of the pointer with a 0x prefix).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜