I understand that:
(NSArray *)methodName
would return an array.
(UIImageView *)methodName
would return an imageview.
But how about if I wanted to return an Array of ImageViews?
If you're worried about it not being very clear about the fact that the array has UIImageViews, you can specify it on the name of the method, something like:
-(NSArray *)imageViewsForSomething:(id)something;
Otherwise, there's no reason you'd need to return explicitly an array of UIImageViews, an NSArray will do the trick.
Arrays and other container objects cannot be type in Objective-C like you can di in java.
One possible solution would be to subclass the NSArray class
NSArray is a stack of memory which stores object you add to it.
To store imagesviews, create an object of imageview and insert it in the array.
If you have to create long list of imageviews, you can create them using some iterative loop and add them in the array.
The array will hold on the imagesviews in it.
I am a newbie here, sorry if maid a mistake in above answer...
Related
I am working on an iphone app in objective-c and I am having trouble with making an array out of a class variable, for example...
I have a class named Cube and I am tryin to make an instance of this class named map that is an array
Cube map[10][10];
Xcode then says it is an error and suggests I do this
Cube *map[10][10];
When I do it this way though ^ I cannot access one of my methods I defined in the class Cube, now this is not ALL my methods, it is just one single method that wont work anytime I try to call it. The only thing different about this method than the others is that I pass a parameter to it. The Cube class declaration and definition both compile flawlessly.
Can anyone explain to me how to make a class with a 2 dimensional without turning it into a pointer. Also why does xcode recommend I make it a pointer, and why doesn't the method work when I do it this way?
You probably want to use a Cocoa style array, i.e. NSArray or NSMutableArray instead of C-style array. It is much more flexible and is designed to work with Objective-c objects. Have a look at this simple tutorial on using a Cocoa array: http://iphonelearning.wordpress.com/2011/08/24/nsarray-and-nsmutablearray/.
The standard approach for this in Objective-C is to create an NSArray(or NSMutableArray) of an NSArray (or NSMutableArray). Assuming you want to be able to manipulate the array after you create the array object, your code would look something like this:
NSMutableArray* cubeGrid = [NSMutableArray new]; // Note that this code assumes you are using ARC.
// add row 1
NSMutableArray* cubeRow1 = [NSMutableArray arrayWithObjects:cube1,cube2,cube3,nil]; // you will need to add cube 4 to 10 in the real code
[cubeGrid addObject:cubeRow1];
// add row 2
NSMutableArray* cubeRow2 = [NSMutableArray arrayWithObjects:cube11,cube12,cube13,nil]; // you will need to add cube 14 to 20 in the real code
[cubeGrid addObject:cubeRow2];
// and you will create the rest of the rows and add to the cubeGrid array
To access the elements you would do something like this:
for (id cubeRow in cubeGrid) {
if ([cubeRow isKindOfClass:[NSArray class]]) {
for (id cube in (NSArray*)cubeRow) {
if ([cube isKindOfClass:[Cube class]]) {
// Do things with cube
}
}
}
}
What you might also want to double check is whether the method that you are trying to access is declared in the header file.
You may want to take a look at this this answer.
Is there a way to tell if a certain object is in an NSArray? The way I am adding objects to my array makes it possible for the same object to be added multiple times and I wanted to see if there was a way to see if it was already there (anywhere) in that array.
The NSArray containsObject: method is precisely for this purpose, its full signature being:
- (BOOL)containsObject:(id)anObject
See the full NSArray Class Reference docs for more information.
if([yourArray indexOfObject:yourObject] == NSNotFound) {
// your object is not in here
}
Edit: middaparkas approach is way better (if you don't want the index …)!
I am using a search bar in my app and display some results below from an ext API.
The contents first get stored in an array "xyz" and each time the user types in the search bar, I removeAllObjects and reload the table.
The results are correct if the user types slow i.e. [xyz removeAllObjects] works fine...However if the user types very fast, [xyz removeAllObjects] does not seem to have any effect and duplicate items get appended to the array..
I am not sure how to fix this. Please help me. Thank you.
removeAllObjects is an entirely serial operation; that method doesn't return until the array is empty.
Therefore, there must be a thread in play and you are quite likely accessing a mutable array from multiple threads. Mutable arrays aren't thread safe. Fix the exclusivity and you'll fix your problem.
The easiest way is to separate the array being displayed from the array being computed. As soon as the computation is done, move the computed array to the display array and never mutate it again.
Why not create a new NSArray, point the results at that, and then release the old array. That way having duplicates will be impossible. Something like:
NSArray *newArray = [someObject newSearchResults];
NSArray *oldArray = xyz;
xyz = [newArray retain];
[oldArray release];
I am trying to update data in a table view using a NSMutableArray. Quite simple :(
What is happening is that I get my data from a NSURLConnection Callback, which I parse and store it in an array and call reload data on the table view. The problem is that when cellForRowAtIndexPath is called back by the framework. The array still shows the correct count of the elements but all string objects I had stored earlier are shown as invalid.
Any pointers
Maybe your problem is something like the below
NSString *value = [NSString stringWithFormat:#"%#",stringFromWebService];
[arrayOfObjects addObject:value];
[value release]; // This should not be released
This may not be exact but could be similar. Here value is obtained from class method of NSString which is an autoreleased object so if you can see it will be released twice, one by autorelease and other by you.
This is the area you need to check.
Check that you are retaining the NSString objects in your NSURLConnection callback. Are you autoreleasing them?
Edit
Ok, forget that last thing. Double checking myself, NSMutableArray will automatically retain the objects when you add them to your array. So you won't need to retain them explicitly:
Like NSArray, instances of
NSMutableArray maintain strong
references to their contents. If you
do not use garbage collection, when
you add an object to an array, the
object receives a retain message. When
an object is removed from a mutable
array, it receives a release message.
So you need to check you aren't doing any other explicit releases on the objects you are adding to the array. Are they referenced anywhere else?
The problem is there where you are adding the string object to a mutable array. The string object was already invalid that time. That's why the time you are accessing them from the array, they are invalid or do not exist.
So best thing is to check the code where you are adding the string object during the parsing.
Your problem may be that you have not initiated the array correctly.
Are you using the array as an attribute of the class?
Make sure when you load the view you allocate and initiate the array.
This seems to be a common problem, but I can't figure out anything from the answers I've seen so far. I have an iPhone app that uses a subclass of NSMutableArray to store objects, plus some additional properties. The subclass is skhCustomArray. The subclass initializes fine, with no objects in the skhCustomArray, and I assign it to the the property of my view controller, which is a pointer to an skhCustomArray.
prescriptionListVC* newPrescList = [[prescriptionListVC alloc] initWithNibName:#"PrescriptionList" bundle:nil];
newPrescList.curPersonPrescriptions = [personDetails objectAtIndex:0];
That works fine. Yet when I push my view managed by my view controller onto the navigation controller stack, the count method in the numberOfRowsInSection method crashes the app, see below.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [curPersonPrescriptions count];
}
What could be causing this? How can a valid custom array, with no objects, not return a valid count? Where am I going wrong? Thanks.
Subclass of NSArray? You're aware that NSArray is a class cluster, and is therefore somewhat difficult to subclass, right? In fact, it's so fraught with danger that the NSArray documentation has a whole section dedicated to what you need to do in order to subclass it.
I'll bet that that's the source of your woes.
You almost certainly don't need to subclass NSMutableArray in this situation. Instead, make a new class which has an array as a property, along with the extra properties you desire.
When you subclass NSMutableArray, you need to implement some mandatory methods like count, addObject:, insertObjectAtIndex etc. This is what we call as class cluster.
If you want to add some more feature/behavior to already implemented object then you can write a "category" instead of "subclassing" it.
If you want to subclass it, then you have to implement all those methods which your are going to use so better write a category of NSMutableArray and extend the feature what you want and use the NSMutableArray object only. This will solve your problem and also this is the easy and almost right way to add new behavior to already existing class.