我在class1中有一个我需要在class2中使用的整数.我在class2的.m文件中导入了class1的.h文件,但我仍然无法访问该变量.不知道为什么!:(
我甚至为class1的.h文件中的每个整数创建了一个属性,并在.m文件中合成它.
谁知道问题是什么?
基本上,这就是我所拥有的 class1.h
//interface here
{
NSInteger row;
NSInteger section;
}
@property NSInteger row;
@property NSInteger section;
Run Code Online (Sandbox Code Playgroud)
这是class1的.m文件.
//implementation
@synthesize section = _section;
@synthesize row = _row;
Run Code Online (Sandbox Code Playgroud)
然后在执行中class2,我有这个
#import "Class2.h"
#import "Class1.h"
Run Code Online (Sandbox Code Playgroud)
如何在类2中的方法中访问这些整数?
您需要创建class1的实例(对象)才能访问属性(变量).
// Create an instance of Class1
Class1 *class1Instance = [[Class1 alloc] init];
// Now, you can access properties to write
class1Instance.intProperty = 5;
class1Instance.StringProperty = @"Hello world!";
// and to read
int value1 = class1Instance.intProperty;
String *value2 = class1Instance.StringProperty;
Run Code Online (Sandbox Code Playgroud)
编辑
// Create an instance of Class1
Class1 *class1Instance = [[Class1 alloc] init];
// Now, you can access properties to write
class1Instance.row = 5;
class1Instance.section = 10;
// and to read
NSInteger rowValue = class1Instance.row;
NSInteger sectionValue = class1Instance.section;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4126 次 |
| 最近记录: |