I only know method addObject, which is add object to the next element in the array. I want to be able to add / set / update object to an arbitrary position at the NSMutableArray, ex:
arr[105] = #(true);
arr[709] = #(30);
arr[1010] = #"Hello world!";
NSLog (#"%#", arr[1010]);
I have been trying something like this, but the next time I tried to retrieve the value, it says nil. How to do this? Thanks.
EDIT: last time I tried, it gave me error: index 1010 beyond bounds for empty array.
you specify a size when you create an array, the specified size is regarded as a “hint”; the actual size of the array is still 0. This means that you cannot insert an object at an index greater than the current count of an array. For example, if an array contains two objects, its size is 2, so you can add objects at indices 0, 1, or 2. Index 3 is illegal and out of bounds.
read more at https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/index.html
below method use for replace existing object at index with new object
[self.arr replaceObjectAtIndex:<#(NSUInteger)#> withObject:<#(nonnull id)#>]
below method use for insert new object at index
[self.arr insertObject:<#(nonnull id)#> atIndex:<#(NSUInteger)#>]
Ex:
if object already available at index and replace with new object
[self.arrayBuyers replaceObjectAtIndex:1010 withObject:#"Hello world!"]
add new object at index
[self.arr insertObject:1 atIndex:#"Hi"];
Related
I use [items indexOfObject:items.lastObject] to get the last index, but this code returns nil. Why does this happen?
The first and last object in your array are both bar button items created with the system item of "fixed space".
The result of calling indexOfObject: is 0, not nil. This means that the object is being found at index 0. indexOfObject: can't return nil. If an object isn't found, it returns the special value NSNotFound which is the unsigned value for -1.
From the documentation for indexOfObject::
Starting at index 0, each element of the array is passed as an argument to an isEqual: message sent to anObject until a match is found or the end of the array is reached. Objects are considered equal if isEqual: (declared in the NSObject protocol) returns YES.
The implementation of UIBarButtonItem isEqual: will return YES if two bar button item instances are created with the same system item (and probably a few other properties as well).
indexOfObject: is not based on the instance of the object, it's based on isEqual:.
If you want to find the index of an object based on the identity (its address) of the object instead of isEqual:, use indexOfObjectIdenticalTo:.
p [items indexOfObjectIdenticalTo:items.lastObject]
will give you 6 instead of 0.
I'm trying to display items from an array in my tableView cells, in a string. Currently, I'm using these lines of code:
.m
id swaptime = self.filteredTotal[0][#"swaptime"];
NSString *test = swaptime;
...to grab the value located in swaptime, and display it in each of my cells. That said, in all of my cells, this line forces them all to display only the FIRST swaptime value - rather than the list of swaptime values from my array in their corresponding cells. Is this because of the [0] in my line (e.g. only grab the value located at 0)? If so, how should it be written instead so that all values are displayed?
I'm not sure of the answer since you have not provided all of your code, but if the code shown above is from the cellForRowAt method of the UITableView datasource, then this should work:
id swaptime = self.filteredTotal[indexPath.row][#"swaptime"];
(provided you do not intend on having more than 1 section)
I am using the following code to store "sqlite3_last_insert_rowid" into NSMutableArray,I am getting the rowid but nothing is stored in the array. It is giving me null.
NSUInteger rowIDNum=sqlite3_last_insert_rowid(myDatabase);
NSNumber *xWrapped = [NSNumber numberWithInt:rowIDNum];
[_rowID insertObject:xWrapped atIndex:0];
NSLog(#"row ID array %#",[_rowID objectAtIndex:0]);
Could you please tell me the correct way to store rowid into an array?
I suspect the array is not allocated, else you would get an NSRangeException if the array was allocated but empty (i.e. the first time you called that method).
From the NSMutableArray reference:
index
The index in the array at which to insert anObject. This value
must not be greater than the count of elements in the array.
Important: Raises an NSRangeException if index is greater than the
number of elements in the array.
You would normally allocate the array in the class init method or viewDidLoad method, depending on what the class is. Once you've allocated the array, use:
NSUInteger rowIDNum=sqlite3_last_insert_rowid(myDatabase);
[_rowID insertObject:#(rowIDNum)];
I have used a Table View with Dynamic Prototype cells.
I have created a sample row in the table view with four values of a quantity:
Title
Min Value
Max Value
Current value.
I have created Table view controller and table view cell classes for accessing those properties in my code.
Now I have to set the values dynamically. I can see people suggesting to pass array of values separately for each.
Like:
Array 1 for Title : which will display title for all items in the row
Array 2 for Min value and so on.....
Is there a way that we pass an array of objects to the table view and it creates the table.
Please suggest.
I can see people suggesting to pass array of values separately for each.
Don't do this it is very poor approach, instead create a single array of NSDictionnaries, for instance:
_listOfValue =
#[
#{#"Title":#"title 1", #"Min Value":#"0", #"Max Value":#"100", #"Current value":#"50"},
#{#"Title":#"title 2", #"Min Value":#"4", #"Max Value":#"90", #"Current value":#"60"},
#{#"Title":#"title 3", #"Min Value":#"6", #"Max Value":#"70", #"Current value":#"70"}
];
This will make it easy to retrieve the data you need since they are not separated.
In numberOfRowsInSection you can return [self.listOfValue count].
In cellForRowAtIndexPath or didSelectRowAtIndexPath you can easily get the dictionnary at the IndexPath then parse the value of each key.
//Get the specific value
NSDictionnary *valueDict = _listOfValue[indexPath.row];
//read data from value dictionary
valueDict[#"Title"];
//or the old syntax
[valueDict objectForKey:#"Title"];
No, you can't pass the array direct to the table. Your table view controller would usually maintain the array (often a single array containing dictionaries or custom class instances) and your code in the table view controller uses that array to configure the cells.
I have set the tags for the buttons but in this method i am getting an exception and i am not sure why
- (IBAction)showComments:(UIButton *)sender
{
int tag=[sender tag];
NSLog(#"The tag clicked:%#",[blogids objectAtIndex:tag]);
}
Where blogids is my NSMutableArray
Thanks
You are getting NSRangeException, which means you are trying to retrieve that element of array which is not existing. I suggest you should check the array count with Tag value which you are trying retrieve.
NSLog(#"%d",[blogids count]);
NSLog(#"%#",tag);
I am sure tag value is greater than count. That should not be, if you want to retrieve value from array using tag.
Thanks,
The reason you are getting an exception is because your tag is greater then the blogids count.
Add the buttons to the array and then it will not crash.
For example:
blogids = [[NSMutableArray alloc]init];
[blogids addObject:oneOfYourButtons];
Also if you only want to see the tags number use this:
NSLog(#"The tag clicked:%d",tag);
instead of:
NSLog(#"The tag clicked:%#",[blogids objectAtIndex:tag]);
Your blogids array is blank. Please check if it has object in the index you got from button tag
Your blogids is empty array here. so that, it show as bounds as [0 .. 0](that is array count is zero). Just check your array initialization.