objective-c - %d 没有正确显示整数

标签 objective-c cocoa cocoa-touch memory-management nslog

当我尝试写作时

NSLog(@"%d", myObject.myId);

其中 myId 为 int,控制台给出一些高数字,如 70614496。当我使用 @"%@" 时,我收到异常 -[CFNumber respondsToSelector :]: 消息发送到已释放的实例 0x466c910。 为什么会这样?

这是 myObject 的定义:

@interface myObject : NSObject {
int myId;
NSString *title;
}

@property(nonatomic) int myId;
@property(nonatomic, retain) NSString *title;

@end

@implementation myObject

@synthesize myId, title;

- (void)dealloc {
[title release];

[super dealloc];
}

@end

最佳答案

卡尔在尝试编译您的代码方面做得很好。关于 %@ 格式说明符的另一句话,因为您的问题似乎意味着一些困惑。

当您在 NSLog 格式中使用 %@ 时,您实际上是在发送参数 description消息,并打印结果字符串。如果myId是一个int而不是NSObject的实例,它不会响应描述消息。使用 %d 表示 int 值,使用 %@ 表示 NSNumber 实例。

关于objective-c - %d 没有正确显示整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1710549/

相关文章:

objective-c - 在 Cocoa 应用程序中使用特定本地化

iphone - 使用委托(delegate)时,需要更好的方法来进行顺序处理

ios - UICollectionView - 如果选择了单元格,则不调用 didDeselectItemAtIndexPath

iphone - MediaPlayer.framework : How to "translate" MPMusicRepeatModeDefault into an actual mode?

iphone - CCSpriteBatchNode : Does the Child of Child receive drawing performance gains?

objective-c - 执行关闭 : on a borderless window?

objective-c - [__NSArrayI replaceObjectAtIndex :withObject:]: unrecognized selector sent to instance

objective-c - UITableView 重新排序隐藏背景

objective-c - iphone改变初始场景

iphone - 检查触摸区域而不仅仅是矩形的更好方法