我对如何在 Objective C 中处理整数感到困惑。
如果我定义以下内容:
NSInteger i = 6;
NSLog(@"%d", i);
我希望它将 6 打印到控制台。
但是我在一个对象中有一个 NSInteger ,它显然是由指针引用的,所以我得到了非常不同的结果。
例如:
@interface Section : NSObject {
NSInteger Id;
}
@property (nonatomic, assign) NSInteger Id;
请假设这已在实现中综合。
我创建对象设置它的值并再次访问它,如下所示:
Section *section = [[Section alloc] init];
section.Id = 6;
NSMutableArray *sections = [[NSMutableArray alloc] init];
[sections addobject:section];
Section *sectionB = [setions objectAtIndex:0];
NSLog(@"%d", sectionB.Id);
这有打印内存地址的奇怪效果,即像 5447889 这样的数字。为什么我不能只得到值?
我试过使用:
NSInteger sid = [[section Id]integerValue];
但随后我收到警告Invalid receiver type 'NSInteger'并且有时会收到错误程序接收信号:“EXC_BAD_ACCESS”。
我真的很想知道如何正确处理整数或任何值。
非常感谢