struct與class區別

OC:

struct Class
不一樣點 值類型 Value Type 引用類型 reference type
結構體只能封裝數據 而類能夠封裝數據,還能夠封裝行爲(實現函數)
結構體變量分配在棧空間 而對象分配在堆空間
賦值爲拷貝 類是指向地址
沒有繼承 有繼承
相同點 均可以將多個數據封裝爲1個總體 均可以將多個數據封裝爲1個總體
struct dataStruct{
	NSInteger age;
	NSInteger year;
	__unsafe_unretained NSString *name;//1.在`ARC`環境下,在結構體中使用`objc`對象,必須使用  `__unsafe_unretained`,這個是蘋果的規定。不然會報錯:ARC forbids Objective-C objects in struct
//	NSDictionary *data;// 2.或者指定單獨
};


[@interface](https://my.oschina.net/u/996807) ClassAndStruct()
{
	NSInteger age;
	NSInteger year;
	NSString *name;
	NSDictionary *data;
}
[@end](https://my.oschina.net/u/567204)

swift

struct Class
不一樣點 值類型 Value Type (不涉及引用計數) 引用類型 reference type
struct更適合當作是一個數據結構的實現體 class更適合當作是一個對象的實現體
struct 沒有繼承 class 有繼承功能

參考文章: OC結構:https://www.yiibai.com/objective_c/objective_c_structures.html OC:http://blog.csdn.net/yi_zz32/article/details/48582339?locationNum=11&fps=1 swift:http://www.javashuo.com/article/p-xstlmvpg-hc.html, http://blog.csdn.net/see_you_in_the_past/article/details/44653249, https://www.jianshu.com/p/8011b638b3a9html

struct與class性能差別:https://www.jianshu.com/p/394e4fe614a0?utm_campaign=maleskine&utm_content=note&utm_medium=writer_share&utm_source=weibo&open_source=weibo_searchswift

相關文章
相關標籤/搜索