Objective C 2.0 属性(Property)
原帖一:http://blog.csdn.net/dotphoenix/archive/2009/05/20/4203075.aspx
原贴二:http://www.cocoachina.com/bbs/read.php?tid-8008.html
@property (参数) 类型 名字;
这里的参数主要分为三类:读写属性(readwrite/readonly),setter语意(assign/retain/copy)以及atomicity(nonatomic)。
assign/retain/copy决定了以何种方式对数据成员赋予新值。我们在声明summary propery时使用了readonly,说明客户端只能对该property进行读取。atomicity的默认值是atomic,读取函数为原子操作。
在objective c 2.0下,由于我们声明了property,implementation文件可以更改如下:
可以看到数据成员title和numofpages的读写函数已经不复存在,取而代之的是两行@synthesize,
见下列示例代码:
//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 <Foundation/Foundation.h>
#import "test.h"
#include <stdio.h>
@implementation MyTest
- (int*)myArray
{
return myArray;
}
- (void)setMyArray:(int*)anArray
{
if(anArray != NULL)
{
for(int i=0; i<5; i++)
myArray[i] = anArray[i];
}
}
这样一来对于数组型变量成员就无法使用点(.)操作符来抽象掉对setter和getter的调用(使用点操作符访问对象的成员数据非常方便,
根据索要访问的数据成员处于左值还是右值而由编译器自动判定调用setter还是getter)。
相关文档:
最近在做一个I2C键盘的Linux驱动,参考了其他芯片的一些代码,其中陆续发现有些让人迷惑的东西,把我的迷惑及理解在这里加以记录:
1. i2c_driver结构体的probe成员的原型:
int (*probe)(struct i2c_client *, const struct i2c_device_id *);
即:probe函数被调用时会从上边传两个个参 ......
C库函数
字符串函数
函数名
函数原型
功能
返回值
包含头文件
strcat
char *strcat(char *st1, char *str2)
把str2连接到str1后面
str1
string.h
strchr
char *strchr(char *str, int ch)
找出str指向的字符串中第一次出现字符串ch的位置
指向该位置的指针,未找到则返回空指针
......
问题源于论坛的一道题目:
http://topic.csdn.net/u/20100216/21/ec98464e-a47e-4263-bb1c-a001e130ba87.html
下面是问题:
设int arr[]={6,7,8,9,10};
int *ptr=arr;
*(ptr++)+=123;
printf("%d,%d",*ptr,*(++ptr));
答案为什么是:8,8
问题的焦点也落在printf执行的问题上,到底先执行谁,后执行谁, 还有部分 ......
Windows C 程序设计入门与提高
http://download.chinaitlab.com/program/files/13246.html
单片机C语言入门
http://download.chinaitlab.com/program/files/12907.html
C++ 入门基础教程
http://download.chinaitlab.com/program/files/7617.html
C语言常用算法源代码
http://download.chinaitlab.com/program/files ......