Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Objective-C have concepts of value/reference type like C#?

Tags:

objective-c

According to the search on Google, looks like it doesn't officially have value/reference type. If it doesn't have, what's the counterparts? What's it called? Is it called 'Obj-C class and non Objc-C class' or 'static type and dynamic type'?

However, to me, a NSString object in obj-c 'works' like a reference type in C#. It'll lead to a compiling error "statically allocated instance of Objective-C class ..." if to define it without '*'

NSString * pstr = [[[NSString alloc] init] autorelease];

Some native types, like NSInteger or C struct, which can be created without '*', can I say it 'works' like a value type in C#?

NSInteger i = 0;
like image 690
Rob L Avatar asked Feb 26 '14 06:02

Rob L


People also ask

Does Objective-C support value type?

There are many kinds of value types in Swift, such as struct , enum , and tuples. You might not realize that Objective-C also uses value types in number literals like NSInteger or even C structures like CGPoint .

Is array value or reference type in Swift?

Use a value type when you want copies to have an independent state, and the data will be used in code across multiple threads. In Swift, Array , String , and Dictionary are all value types.

Are Dictionaries reference types in Swift?

In Objc string, array and dictionary are all reference types, while in Swift they are all value types.

What is difference between value type and reference type in C#?

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.


1 Answers

All Objective-C objects are accessed by a pointer. In that sense they are all reference types.

Some Objective-C types define object equality (the -isEqual: method) as something other than pointer equality. These types behave as value-like types. Such types include NSString and NSNumber.

Types that are not Objective-C classes can be true value types in the C# sense.

like image 86
Greg Parker Avatar answered Oct 20 '22 01:10

Greg Parker