@代表“Objective-C”的标志,证明您正在使用Objective-C语言
Objective-C语言关键词,@property与@synthesize配对使用。
功能:让编译好器自动编写一个与数据成员同名的方法声明来省去读写方法的声明。
如:
1、在头文件中:
@property int count;
等效于在头文件中声明2个方法:
- (int)count; -(void)setCount:(int)newCount;
2、实现文件(.m)中
@synthesize count;
等效于在实现文件(.m)中实现2个方法。
- (int)count
{
return count;
}
-(void)setCount:(int)newCount
{
count = newCount;
}
以上等效的函数部分由编译器自动帮开发者填充完成,简化了编码输入工作量。
格式:
声明property的语法为:@property (参数1,参数2) 类型 名字;
如:
@property(nonatomic,retain) UIWindow *window;
其中参数主要分为三类:
读写属性: (readwrite/readonly)
setter语意:(assign/retain/copy)
原子性: (atomicity/nonatomic)
各参数意义如下:
readwrite: 产生setter\getter方法
readonly: 只产生简单的getter,没有setter。
assign: 默认类型,setter方法直接赋值,而不进行retain操作
retain: setter方法对参数进行release旧值,再retain新值。
copy: setter方法进行Copy操作,与retain一样
nonatomic: 禁止多线程,变量保护,提高性能
参数类型
参数中比较复杂的是retain和copy,具体分析如下:
getter 分析
1、
@property(nonatomic,retain)test* thetest; @property(nonatomic ,copy)test* thetest;
等效代码:
-(void)thetest
{
return thetest;
}
2、
@property(retain)test* thetest; @property(copy)test* thetest;
等效代码:
-(void)thetest
{
[thetest retain];
return [thetest autorelease];
}
setter分析
1、
@property(nonatomic,retain)test* thetest; @property(retain)test* thetest;
等效于:
-(void)setThetest:(test *)newThetest {
if (thetest!= newThetest) {
[thetestrelease];
thetest= [newThetest retain];
}
}
2、
@property(nonatomic,copy)test* thetest; @property(copy)test* thetest;
等效于:
-(void)setThetest:(test *)newThetest {
if (thetest!= newThetest) {
[thetest release];
thetest= [newThetest copy];
}
}
nonatomic
如果使用多线程,有时会出现两个线程互相等待对方导致锁死的情况(具体可以搜下线程方面的注意事项去了解)。在没有(nonatomic)的情况下,即默认(atomic),会防止这种线程互斥出现,但是会消耗一定的资源。所以如果不是多线程的程序,打上(nonatomic)即可
retain
代码说明
如果只是@property NSString*str; 则通过@synthesize自动生成的setter代码为:
-(void)setStr:(NSString*)value{
str=value;
}
如果是@property(retain)NSString*str; 则自动的setter内容为:
-(void)setStr:(NSString*)v{
if(v!=str){
[str release];
str=[v retain];
}
}
所有者属性
我们先来看看与所有权有关系的属性,关键字间的对应关系。
属性值 关键字 所有权 strong __strong 有 weak __weak 无 unsafe_unretained __unsafe_unretained 无 copy __strong 有 assign __unsafe_unretained 无 retain __strong 有 strong该属性值对应 __strong 关键字,即该属性所声明的变量将成为对象的持有者。
weak该属性对应 __weak 关键字,与 __weak 定义的变量一致,该属性所声明的变量将没有对象的所有权,并且当对象被破弃之后,对象将被自动赋值nil。
并且,delegate 和 Outlet 应该用 weak 属性来声明。同时,如上一回介绍的 iOS 5 之前的版本是没有 __weak 关键字的,所以 weak 属性是不能使用的。这种情况我们使用 unsafe_unretained。
unsafe_unretained等效于__unsafe_unretaind关键字声明的变量;像上面说明的,iOS 5之前的系统用该属性代替 weak 来使用。
copy与 strong 的区别是声明变量是拷贝对象的持有者。
assign一般Scalar Varible用该属性声明,比如,int, BOOL。
retain该属性与 strong 一致;只是可读性更强一些。
参考:
http://blog.eddie.com.tw/2010/12/08/property-and-synthesize/
http://www.cocoachina.com/bbs/read.php?tid=7322
http://www.cnblogs.com/pinping/archive/2011/08/03/2126150.html
声明的分类
在 Objective-C官方文档 中的Property一章里有对类Property详细说明。
@property中的声明列表已分类为以下几种:
1, 声明属性的访问方法:
- getter=getterName
-
setter=setterName
声明访问属性的设置与获取方法名。
2,声明属性写操作权限:
-
readwrite
声明此属性为读写属性,即可以访问设置方法(setter),也可以访问获取方法(getter),与readonly互斥。 -
readonly
声明此属性为只读属性,只能访问此属性对应的获取方法(getter),与readwrite互斥。
3,声明写方法的实现:
-
assign
声明在setter方法中,采用直接赋值来实现设值操作。如:
-(void)setName:(NSString*)_name{
name = _name;
}
-
retain
声明在setter方法中,需要对设过来的值进行retain 加1操作。如:
-(void)setName:(NSString*)_name{
//首先判断是否与旧对象一致,如果不一致进行赋值。
//因为如果是一个对象的话,进行if内的代码会造成一个极端的情况:当此name的retain为1时,使此次的set操作让实例name提前释放,而达不到赋值目的。
if ( name != _name){
[name release];
name = [_name retain];
}
}
-
copy
调用此实例的copy方法,设置克隆后的对象。实现参考retain。
4,访问方法的原子性:
-
nonatomic
在默认的情况下,通过synthesized 实现的 setter与getter 都是原子性访问的。多线程同时访问时,保障访问方法同时只被访问一个线程访问,如: -
[ _internal lock ]; // lock using an object-level lock id result = [ [ value retain ] autorelease ]; [ _internal unlock ]; return result;
-
但如果设置nonatomic时,属性的访问为非原子性访问。
来源:http://wiki.magiche.net/pages/viewpage.action?pageId=1540101
@synthesize tabBarController=_tabBarController;
@synthesize 中可以定义 与变量名不相同的getter和setter的命名,籍此来保护变量不会被不恰当的访问
ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码:
以上代码是实现异步获取图片的主方法,SoftReference是软引用,是为了更好的为了系统回收变量,重复的URL直接返回已有的资源,实现回调函数,让数据成功后,更新到UI线程。
几个辅助类文件:
ViewCache是辅助获取adapter的子元素布局
ImageAndTextListAdapter是实现ListView的Adapter,里面有个技巧就是
imageView.setTag(imageUrl),setTag是存储数据的,这样是为了保证在回调函数时,listview去更新自己对应
item,大家仔细阅读就知道了。
最后贴出布局文件:
原文地址:http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2
- AsyncListImage.zip (50.5 KB)
主程序:
private List<String> items = null;
private List<String> paths = null;
private String rootPath = "/";
private TextView mPath;
private ListView listView1;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.file_browser);
mPath = (TextView) findViewById(R.id.textView1);
listView1 = (ListView) findViewById(R.id.listView1);
getFileDir(rootPath);
}
/* 取得文件架构的method */
private void getFileDir(String filePath) {
/* 设定目前所在路径 */
mPath.setText(filePath);
items = new ArrayList<String>();
paths = new ArrayList<String>();
File f = new File(filePath);
File[] files = f.listFiles();
if (!filePath.equals(rootPath)) {
/* 第一笔设定为[回到根目录] */
items.add("返回根目录");
paths.add(rootPath);
/* 第二笔设定为[回上层] */
items.add("返回上一层");
paths.add(f.getParent());
}
/* 将所有文件加入ArrayList中 */
for (int i = 0; i < files.length; i++) {
File file = files[i];
items.add(file.getName());
paths.add(file.getPath());
}
/*
* 声明一ArrayAdapter,使用file_row这个Layout, 并将Adapter设定给此ListActivity
*/
MyListAdapter myListAdapter = new MyListAdapter(this ,items , paths);
listView1.setAdapter(myListAdapter);
listView1.setOnItemClickListener(new ListView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
File file = new File(paths.get(arg2));
if (file.canRead()) {
if (file.isDirectory()) {
/* 如果是文件夹就再进去读取 */
getFileDir(paths.get(arg2));
} else {
openFile(file);//自定义打开文件的方法
}
} else {
/* 弹出AlertDialog显示权限不足 */
new AlertDialog.Builder(IconFileBrowser.this)
.setTitle("Message")
.setMessage("权限不足!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
}
}).show();
}
}
});
MyListAdapter.class
public class MyListAdapter extends BaseAdapter{
private Context context;
private List<String> fileName , filePath;
public MyListAdapter(Context context , List<String> fileName , List<String> filePath){
this.context = context;
this.fileName = fileName;
this.filePath = filePath;
}
public int getCount() {
return fileName.size();
}
public Object getItem(int position) {
return fileName.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);//LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.list_item3 , null);
ImageView image = (ImageView) layout.findViewById(R.id.imageView1);
File file = new File(filePath.get(position));
if(file.isDirectory()){
image.setImageResource(R.drawable.folder);
}
else image.setImageResource(R.drawable.doc);
TextView title = (TextView) layout.findViewById(R.id.textView1);
title.setText(fileName.get(position));
return layout;
}
}
list_item3.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView1" android:src="/blog_article/@drawable/icon/index.html" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout> </LinearLayout>
识别文件类型并打开
private void openFile(File f) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
/* 调用getMIMEType()来取得MimeType */
String type = getMIMEType(f);
/* 设定intent的file与MimeType */
intent.setDataAndType(Uri.fromFile(f), type);
startActivity(intent);
}
/* 判断文件MimeType的method */
private String getMIMEType(File f) {
String type = "";
String fName = f.getName();
/* 取得扩展名 */
String end = fName
.substring(fName.lastIndexOf(".") + 1, fName.length())
.toLowerCase();
/* 依扩展名的类型决定MimeType */
if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
type = "audio";
} else if (end.equals("3gp") || end.equals("mp4")) {
type = "video";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
|| end.equals("jpeg") || end.equals("bmp")) {
type = "image";
} else {
type = "*";
}
/* 如果无法直接打开,就弹出软件列表给用户选择 */
type += "/*";
return type;
}
重命名文件
file.renameTo(new File(newPath));
删除文件
file.delete();