比如說有這樣一段代碼:
在.h中給了一個類的這個屬性
@interface groupmodel : NSObject
@property (nonatomic,copy)NSArray* friends;
然后在.m中寫了一個set的方法:
-(void)setFriends:(NSArray *)friends
{
NSMutableArray* data = [NSMutableArray array];
for(NSDictionary* dictdata in friends)
{
groupmodel* model = [[groupmodel alloc] init];
[model setValuesForKeysWithDictionary:dictdata];
[data addObject:model];
}
_friends = [data copy];
}
請問這種情況的時候,nsarray* friends一開始并沒有分配資料空間大小嗎?還是什么機制呢?謝謝。
uj5u.com熱心網友回復:
蘋果的屬性宣告,@property機制默認會幫你建立一個_開頭,同名的內部變數并默認生成get和set方法(外部都是通過這兩個方法訪問的).
你的屬性名字叫friends,所以有個內部變數叫_friends
你重寫了-(void)setFriends:(NSArray *)friends方法時,在方法內創建了一個陣列
NSMutableArray* data = [NSMutableArray array];
這里已經分配了空間了
在最后你把這個屬性_friends指向了這個陣列data
_friends = [data copy];
所以你的friends就是你方法里的data,這樣就知道空間哪里來的吧
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/131569.html
標籤:iOS
上一篇:學APP跳轉出現了問題
