What is the meaning of the "mutable" in objective C? - objective-c

I want to ask a question about the objective C. When I study the library from the apple developer website. I always see that there are some subclass called "mutable". For example, the NSArray and NSMutableArray. What does it mean about this word. Are there some special meaning? Can anyone tell me? Thank you.

It means you can change its values. If you look at the NSMutableArray docs, you'll see it defines extra methods like -addObject:. NSArray by itself doesn't have these (and can thus be more efficient / take less memory in implementation).
Also note, if you call [myMutableArray copy] you'll get a non-mutable copy of it (which you must later release0. And similarly there's -mutableCopy.

Mutable means you can change it. Look at the difference between addObject in NSMutableArray and arrayByAddingObject in NSArray.

From the Objective-C Beginner's Guide it states the answer to your specific question:
There are two kinds of arrays (and of
usually most data oriented Foundation
classes) NSArray and NSMutableArray.
As the name suggests, Mutable is
changable, NSArray then is not. This
means you can make an NSArray but once
you have you can't change the length.
This tech note also implies you can change the length of a mutable array after the array has been created.
In general mutability stems from these meanings. This will help provide a more broad understanding for when you encounter it elsewhere.

Related

Object creation using NSNumber literals

what is the difference between these two approaches for objection creation?
NSNumber *aInt= #54;
NSNumber *aInt= [NSNumber numberWithInt:54];
I know that first approach is NSNumber literal and second is NSNumber class methods. Both are used to create an NSNumber object. But which approach is better?
if i creates object using literal style then how memory is allocated to it? How it is differ then second approach? Please suggest me some key point about these concepts so that i prefer better approach to create NSNumber object.
I know that object created at runtime. Does literals objects are also created at runtime? please provide some essential key points to clear my doubt.
They are identical. In fact they are translated to the same code during compilation.
I always go for the literal because it's easier to read.
What is the difference between these two approaches for objection
creation?
They are Identical.
Answers for:
No. 1:
Using #-prefix will automatically wrap scalar types like int or float.
No. 2:
Using numberWith[type] creates NSNumber specifically and therefor easy to distinguish.
I'm not really sure about memory management of it, but i think, talking about memory allocation it's just the same.
I personally use numberWith[type] because it's more distinguishable (for me), i mean the types it holds and i dont have to worry about backward compatibility, but still i have the knowledge about the literals so i dont have any issue using it.
Better approach is very hard to answer, it's completely up to you/to the developer.
You might also want to check this link
And please, avoid asking too many questions in one post. Cheers! :)

Performance of mutable versus immutable objects

I would like to ask all the Cocoa veterans out there - is there any difference in performance between using mutable versus immutable objects in cases like:
NSString's stringByAppendingString: versus NSMutableString's appendString:
NSArray's arrayByAddingObject: versus NSMutableArray's addObject:
...
Thank you and happy coding!
This question is hard to answer : NSArray and NSString aren't actual implementations, they are class-clusters and so are NSMutableArray and NSMutableString. The true implementations underneath can't be determined and thus performances would be hard to compare.
You probably won't find a definite answer to that one.
But what I would guess is : stringByAppendingString and arrayByAddingObject create new objects which contains the modifications, ie copy the current items to a new place in memory, NSMutableArray and NSMutableString should have better performances because they are built to prevent copying when possible (not actually true because NSMutableArray might recopy memory when elements are added but not every time).
I think you should trust the CoreFoundation coders on this one : you wan't to mutate objects ? Use the mutables one.

C style array in Objective-C

With all the useful things you get from NSArray and NSMutableArray, why would you ever use a "C Style" array with Objective-C objects?
NSString *array[] = {#"dog", #"cat", #"boy"};
For short, fixed arrays, the availability of a nice compact initialization syntax (as you've demonstrated) can be nice. In certain cases, a C style array may also offer a performance benefit compared to using NSArray. Another thing that comes to mind is that NSArray doesn't offer any built in support for multidimensional arrays, while multidimensional C arrays are easy.
And of course, there's the fact that you can only store objects in NSArray, not C-primitive types, but you asked specifically about using C arrays with Objective-C objects.
All useful things come at a price.
Ever try making an NSArray of ints? You can't. You MUST use NSNumber.
Why? Because NSArray doesn't know what to do with things it doesn't know how to memory-manage... and the only things that conform to it's memory-management requirements are things based on NSObject.
It's just one example, but there are plenty of others.
Bottom line is, there is rarely ever one single "best" answer to anything. NSArray is no exception.

comparing NSCFString and NSCFBoolean

I am using SBJsonParser to parse JSON. An input can be 0 or a string (ex. a829d901093), and if it's zero, NSCFBoolean is returned, if its string NSCFString is returned. How can I tell which one is returned?
Thanks!
Calling these by their internal toll-free names is what makes this confusing. If you call them NSNumber and NSString (as they are listed in the documentation), then the answer is clear:
if ([value isKindOfClass:[NSNumber class]]) { ... }
EDIT: #Magnus points out that it isn't obvious that a NSCFBoolean isa NSNumber in order to look them up. That's true. To me it's very obvious because I know the Core Foundation type system and I know what the toll free bridge classes mean and how they're implemented (it's one of the coolest tricks in all of Cocoa IMO). But what if you didn't know those things? It's still no problem.
In the debugger, look in the variable list and expand the variable you care about. Its first "member" will be it superclass. Expand. Continue until you find a class you know.
Alternately, you can walk the superclasses using NSStringFromClass([object superclass]). Keep tacking superclass on for as many levels as you'd like to check.

cannot convert 'b2PolygonShape' to 'objc_object*' in argument passing

I am not sure if many of you are familiar with the box2d physics engine, but I am using it within cocos2d and objective c.
This more or less could be a general objective-c question though, I am performing this:
NSMutableArray *allShapes = [[NSMutableArray array] retain];
b2PolygonShape shape;
..
..
[allShapes addObject:shape];
and receiving this error on the addObject definition on build:
cannot convert 'b2PolygonShape' to 'objc_object*' in argument passing
So more or less I guess I want to know how to add a b2PolygonShape to a mutable array. b2PolygonShape appears to just be a class, not a struct or anything like that. The closest thing I could find on google to which I think could do this is described as 'encapsulating the b2PolygonShape as an NSObject and then add that to the array', but not sure the best way to do this, however I would have thought this object should add using addObject, as some of my other instantiated class objects add to arrays fine.
Is this all because b2PolygonShape does not inherit NSObject at it's root?
Thanks
b2PolygonShape is a C++ class, not an ObjC class. You can only put ObjC instances into "NS-containers".
Since you need C++ anyway, it's better to use a std::vector<b2PolygonShape>.
NS-container classes can (as KennyTM pointed out) only store NSObjects. This can be a bit of a pain sometimes. But there are plenty of alternatives to NS-containers.
You can write Objective-C wrapper classes (or use NSValue), and store these in an NSArray.
You could use a plain old C array (though, that may not serve your needs, if the array size is undefined and shrinks and grows)
You could use a hash table to store your references.
A linked list of structs can also come in handy, and is fairly easy to create and maintain.
Should you decide to stick to std::vector, which is as good a solution as any, you can read more about that at: http://www.cplusplus.com/reference/stl/vector/