Objective-C 2D arrays [duplicate] - objective-c

This question already has answers here:
Multi-dimensional NSArray object
(3 answers)
Closed 8 years ago.
In objective C how can I add specific numeric values to an array at given points (for example row3 column6) And how can I then also retrieve that value to compare it to another array at that same given point? When I try using examples I found online I get memory leaks so how do you all initiate them? What's the probably allocating / deallocating methods, etc.

Objective c is a super set of c so you can follow all the same rules of c programming for declaring and using arrays. ARC (if using ios5) will not manage the memory for these objects because they are not objective c objects so you'll have to manually allocate and destroy the objects yourself. This tutorial should get you started:
http://www.cprogramming.com/tutorial/lesson8.html

Related

Create Objective-C literal object like NSString [duplicate]

This question already has answers here:
Can the new Clang Objective-C literals be redirected to custom classes?
(2 answers)
Closed 6 years ago.
I want to know if it's possible to create Objective-C literals like NSString, where instead of [[Object alloc] init], and then assigning you can just assign a value to it, such as #"A string".
Obviously NSString is an object because it has methods to manipulate the data in addition, so in theory there should be a way to do it yourself, but I'm not sure where to even go about finding stuff like this.
Objective-C is C. The primitive (what I would call scalar) data types are all numbers and are completely defined by the language; you cannot add to them (though you can rename them using typedef. The corresponding literals, such as 1 and "hello", are also part of C.
Similarly, literals like #"howdy" and #[#"howdy"], though defined by Objective-C rather than C, are part of the language and you cannot change or add to them, as the literal syntax is built into the language.

Creating a Map<Object,Array<Object>> in objective c [duplicate]

This question already has answers here:
What is the Java equivalent of Objective-C's NSDictionary?
(3 answers)
Closed 9 years ago.
I am interested in creating the java equivalent of a Map data structure that looks as follows:
Map <Object ---> NSMutableArray of objects>
or
Map<Object,Array<Object>>
Can anyone provide guidance on what would be the best way of doing this in objective c as I am fairly new to the language.
Objective-c does not have typed collections. You just create NSMutableDictionary instance, and put NSMutableArray into the values.

Bracket syntax in Objective-C [duplicate]

This question already has answers here:
What are the details of "Objective-C Literals" mentioned in the Xcode 4.4 release notes?
(4 answers)
Closed 9 years ago.
I like to mess with coding every now and then as a hobby and I noticed an unfamiliar syntax in some of the Apple Developer documentation:
newSectionsArray[index]
I normally expect something like:
[object method]
Can anyone explain this to me?
Thanks!
It's called object subscripting, as explained here
Its syntactic sugar, as
newSectionsArray[index]
gets translated by the compiler to
[newSectionsArray objectAtIndexedSubscript:index];
NSDictionary implements subscripting too, so you can access an element in this fashion:
dictionary[#"key"]
The cool (and potentially dangerous) feature is that this is generalized, so you can even have it on your own classes.
You just need to implement a couple of methods
(for indexed access)
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
or (for keyed access)
- (id)objectForKeyedSubscript:(id)key;
- (void)setObject:(id)obj forKeyedSubscript:(id)idx;
and you they will be called whenever you use bracket notation on the instances of you custom class.
So you could end up coding a grid-based game and accessing the elements on the grid by
board[#"B42"]; // => [board objectForKeyedSubscript:#"B42"]
or moving a piece on the board by
board[#"C42"] = #"Troll"; => [board setObject:#"Troll" forKeyedSubscript:#"C42"];
Nice, but I wouldn't abuse of it.
That's literal syntax, introduced in Clang 3.4. You could however use the old syntax [newSectionsArray objectAtIndex:index]. it's the same thing.
newSectionsArray is probably an array (i.e. a contigous block of multiple objects of the same type) and index an integer. newSectionsArray[index] gives you the object at position index (starting counting with 0).

Mutable array object types [duplicate]

This question already has answers here:
NSMutableArray - force the array to hold specific object type only
(12 answers)
Closed 9 years ago.
I'm programming on Objective C for the first time, coming from C++ (so far I like
the latter much better!). I have a question regarding mutable arrays, namely I want to create one with the specific type of one of my objects, 'CMParticle', instead of the generic ID type. To access data in my object from my mutable array, I have to cast it as one of my objects each time (which is I believe cumbersome) like so:
rij[0] = ((CMParticle *)particles[*pi]).crds[0] - ((CMParticle *)particles[*pj]).crds[0];
where 'particles' is my mutable array of CMParticle objects. I would rather do this
rij[0] = particles[*pi].crds[0] - particles[*pj].crds[0];
Prior to this I declare my mutable array like so:
particles = [NSMutableArray array];
It would be nice if I could declare this array with my type somehow so I don't have to typecast every time. Is there a way to do this?
What you're trying to do doesn't actually make sense in Objective C.
C++ containers are homogenous, but generic. You can have a vector<CMParticle>, or a vector<int>, and they're different types.
ObjC containers are heterogeneous. You just have an NSArray, and it can hold CMParticle objects, NSNumber objects, or anything else, all mixed up in one big array.
You generally don't need these casts at all. If you want to send a message to my_array[3], just do [my_array[3] doSomething:15]. Just like a higher-level language (Python, Ruby, Smalltalk, Javascript, etc.).
The only problem is that (unlike Python, etc.), there are a few cases where you do need the cast. Most critically (and annoyingly), if you want to access members directly, you have to cast first. This is one of the reasons that ObjC (unlike Python, etc.) encourages you to use #property and/or explicit accessors instead of directly accessing members. (Also, as a more minor annoyance, because variables have declared types, you can't just write tempval = my_array[3];, you have to specify the type, like: CMParticle *tempval = my_array[3].)
Another way to look at this: C++ extends C's static, weak type system to give you a stronger static type system; ObjC instead bolts on a separate dynamic type system (unfortunately leaving the existing C stuff unchanged, which is where the occasional problems come in).
You can pretty easily write your own NSMutableArray subclass that's generic (taking the class at runtime, unlike C++'s compile time, of course) and homogenous, but all that does is add restrictions; the elements will still be id everywhere. The only way around that is to write a custom class for each array: MutableCMParticleArray, MutableNSNumberArray, etc.

Sorting an array in objective-c [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
sorting in nsmutable array
I have 2 NSMutableArray array. I have to sort it. Is there any function defined in Objective-c for direct sort.
like
NSMutableArray *sortedarray_variable= (sorting method) array_variable;
Where sorting method returns sorted array.
Of course. NSArray provides several methods