在objective-C类中声明一个数组型成员变量的property
2014-01-13 02:21
495 查看
我在做OpenGL的一个小测试程序时碰到需要定义数组的情况,然后就在一个objc类中定义了一个数组,不过后面问题来了,我该如何为它声明property呢?请见下列示例代码:
//test.h
@interface MyTest : NSObject {
int myArray[5];
}
@end
如果采用
@property int myArray[5];
//test.h
@interface MyTest : NSObject {
int myArray[5];
}
- (void)outPutValues;
@property int* myArray;
@end
即,使用指针来表示返回类型并作为参数设置类型。
不过这样一来就无法在.m文件的实现中使用@synthesize,而是需要显式地实现这对方法:
#import
#import "test.h"
#include
@implementation MyTest
- (int*)myArray
{
return myArray;
}
- (void)setMyArray:(int*)anArray
{
if(anArray != NULL)
{
for(int i=0; i<5; i++)
myArray[i] = anArray[i];
}
}
- (void)outPutValues
{
int a[5];
for(int i=0; i<5; i++)
printf("%d ", (myArray)[i]);
}
@end
int main (int argc, const char *
argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
int a[] = { [4] = 100 };
MyTest *myTest = [[MyTest alloc] init];
[myTest setMyArray:a];
NSLog(@"The fifth value is: %d", [myTest myArray][4]);
[myTest outPutValues];
[myTest release];
[pool drain];
return 0;
}
这样一来对于数组型变量成员就无法使用点(.)操作符来抽象掉对setter和getter的调用(使用点操作符访问对象的成员数据非常方便,根据索要访问的数据成员处于左值还是右值而由编译器自动判定调用setter还是getter)。
另外,setMyArray的参数类型可以是const:
- (void)setMyArray:(const int*)anArray
转自 http://blog.sina.com.cn/s/blog_7d35fa1a0101htu9.html
//test.h
@interface MyTest : NSObject {
int myArray[5];
}
@end
如果采用
@property int myArray[5];
肯定会出错。 因为@property的声明指示编译器默认地给myArray添加了myArray以及setMyArray的这样一对getter和setter方法。由于objective-C中方法的返回类型不能是数组,所以上述声明property的方式是通不过编译的。 正确的方式是:
//test.h
@interface MyTest : NSObject {
int myArray[5];
}
- (void)outPutValues;
@property int* myArray;
@end
即,使用指针来表示返回类型并作为参数设置类型。
不过这样一来就无法在.m文件的实现中使用@synthesize,而是需要显式地实现这对方法:
#import
#import "test.h"
#include
@implementation MyTest
- (int*)myArray
{
return myArray;
}
- (void)setMyArray:(int*)anArray
{
if(anArray != NULL)
{
for(int i=0; i<5; i++)
myArray[i] = anArray[i];
}
}
- (void)outPutValues
{
int a[5];
for(int i=0; i<5; i++)
printf("%d ", (myArray)[i]);
}
@end
int main (int argc, const char *
argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
int a[] = { [4] = 100 };
MyTest *myTest = [[MyTest alloc] init];
[myTest setMyArray:a];
NSLog(@"The fifth value is: %d", [myTest myArray][4]);
[myTest outPutValues];
[myTest release];
[pool drain];
return 0;
}
这样一来对于数组型变量成员就无法使用点(.)操作符来抽象掉对setter和getter的调用(使用点操作符访问对象的成员数据非常方便,根据索要访问的数据成员处于左值还是右值而由编译器自动判定调用setter还是getter)。
另外,setMyArray的参数类型可以是const:
- (void)setMyArray:(const int*)anArray
转自 http://blog.sina.com.cn/s/blog_7d35fa1a0101htu9.html
相关文章推荐
- 在objective-C类中声明一个数组型成员变量的property
- 在objective-C类中声明一个数组型成员变量的property
- 在objective-C类中声明一个数组型成员变量的property
- Objective-C成员变量声明方式探究
- 【objective-c】内存管理中成员变量声明参数的一些总结
- 作业题,编写两个类:A和B,A创建的对象可以计算两个正整数的最大公约数,B创建的对象可以计算两个数的最小公倍数,要求:B类中有一个成员变量是A类声明的对象
- Objective-C 声明一个C语言类型的数组和访问方式@property
- 创建一个Rectangle类,添加winth和height两个成员变量.........
- 【转】static和extern的区别;当使用另外一个文件中的变量、类、方法时,什么时候改用extern声明,什么时候该include?
- 定义一个空类型,里面没有任何成员变量和成员函数。对该类型求sizeof 结果?
- java中使用反射做一个工具类,来为指定类中的成员变量进行赋值操作,使用与多个类对象的成员变量的赋值。
- 定义一个包含私有成员变量和函数的类,再定义一个内部类,在内部类函数中访问外部成员变量,并调用外部函数。在外部类函数中创建内部类对象,调用内部类函数
- objective-c中点语法和成员变量作用域
- Java构造时成员初始化的陷阱(Java中的声明和初始化不是一个原子操作)
- 尝试创建一个父类和子类,分别创建一个构造方法,然后向父类和子类添加成员变量和方法,并总结构造子类对象时的顺序。
- 如何声明一个全局变量,可以在应用的任何单元引用
- 创建一个 Rectangle类,添加width和lenght两个成员变量 在 Rectangle类中添加两种方法分别计算矩形的周长和面积 编程利用Rectangle输出一个矩形的周长和面积
- 设计一个电视机类,成员变量包括电视机编号、生产厂家、品牌名称、大小、开关状态,并可以对其进行相应操作,如打开电视、提高/降低音量、更换频道等。
- objective-c 成员变量与属性
- 编写一个学生类Students,该类成员变量包括学号no、姓名name、性别sex和年龄age,该类的成员方法有genNo()getName ). * getSex().getAge().和setA