Xcode基础:声明具有整数属性的自定义类,并在另一个类中使用它

Xcode基础:声明具有整数属性的自定义类,并在另一个类中使用它,xcode,class,properties,integer,Xcode,Class,Properties,Integer,尝试做一些非常简单的事情,但无法理解语法 我有一个叫做Word.h的类,它有8个属性,字符串和整数。为了简单起见,这里我将坚持使用2: #import <UIKit/UIKit.h> @interface Word : NSObject @property (nonatomic, strong) NSString *word; @property (nonatomic, strong) NSNumber *wordLevel; @end 在.m文件中,这是: Word *newW

尝试做一些非常简单的事情,但无法理解语法

我有一个叫做Word.h的类,它有8个属性,字符串和整数。为了简单起见,这里我将坚持使用2:

#import <UIKit/UIKit.h>
@interface Word : NSObject
@property (nonatomic, strong) NSString *word; 
@property (nonatomic, strong) NSNumber *wordLevel;
@end
在.m文件中,这是:

Word *newWord = [[Word alloc] init];
   [newWord setWord:@"theorise"];
   [newWord setWordLevel:6];

Word *newWord1 = [[Word alloc] init];
   [newWord setWord:@"implicit"];
   [newWord setWordLevel:7];

Word *newWord2 = [[Word alloc] init];
   [newWord setWord:@"incredible"];
   [newWord setWordLevel:9];
我现在收到一条错误消息“ARC不允许将'int'隐式转换为'NSNumber*'”

我做错了什么…类文件中的属性定义不正确吗??如何访问此属性。它和绳子配合得很好

我还想稍后访问属性-我该如何做…例如:

cell.label1.text = [newWord2 wordLevel];
这是正确的语法吗

希望有人能帮帮我,把一堆头发扯出来!
M

您将
wordLevel
声明为一个
NSNumber
,一个对象。您在代码中对待它就像对待普通的C
int
。你必须决定你想要的是什么,并以一贯的方式对待它。例如,对于普通的C
int
属性,您可以声明:

@property (nonatomic, assign) int wordLevel;
另一方面,如果您确实希望
wordLevel
成为
NSNumber
的话,您需要像这样使用setter:

[newWord setWordLevel:[NSNumber numberWithInt:6]];
[newWord setWordLevel:[NSNumber numberWithInt:6]];