Unable to store sqlite3_last_insert_rowid into an array - ios

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)];

Related

Objective C - NSArray's indexOfObject: not work

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.

How to add object / value to a certain element on NSMutableArray?

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"];

How to replace object index in NSMutableArray in iPhone

I have two different arrays and I want to check firstArray objects and accordingly insert objects in second array.If my firstArray contains particular object then at that index, I am trying insert value in secondArray.
Currently, I am inserting values like :
[secondArray replaceObjectAtIndex:0 withObject:transIdArray];
[secondArray replaceObjectAtIndex:1 withObject:fullCaseArray];
[secondArray replaceObjectAtIndex:2 withObject:caseTitleArray];
[secondArray replaceObjectAtIndex:3 withObject:filingDateArray];
My problem is, If in firstArray transIdArray is at 2 index then my these two arrays data getting mismatched.Please suggest me better way to check add insert values in arrays. Thanks.
NSArray elements are naturally packed together, not sparse, like C arrays can be. To accomplish what you want, the secondArray needs to carry placeholders that are considered non-objects semantically by your app. [NSNull null], an instance of NSNull (not to be confused with nil) is a common choice.
You could initialize one or both arrays like this:
for (NSInteger i=0, i<SOME_KNOW_MAX_LENGTH; ++i) {
[secondArray addObject:[NSNull null]];
}
Then an instance of NSNull in the second array means 'there's nothing at this index in this array corresponding to firstArray'. And you can check if that condition holds for a given index like this:
id secondArrayElement = secondArray[index];
if ([secondArrayElement isMemberOfClass:[NSNull class]]) { // ...
As an aside - often, when I find myself needing to coordinate parallel arrays, it usually means I have some undone representational work, and what I really need is a single array with a more thoughtful object, or the containing object must be more thoughtfully designed.

NSMutableArray Extra Nil Sentinels

I've read a text file into an array of strings. In the below code I'm creating objects from that array and adding them into an NSMutableArray.
NSMutableArray* metaphors = [[NSMutableArray alloc] init];
unsigned int i, cnt = [allLinedStrings count];
for(i = 0; i < cnt-3; i+=5)
{
Metaphor *newMetaphor = [[Metaphor alloc] init];
[newMetaphor setMetaphorTitle: allLinedStrings[i]];
[newMetaphor setCorrectAnswer: allLinedStrings[i+1]];
[newMetaphor setLiteralAnswer: allLinedStrings[i+2]];
[newMetaphor setWayOffAnswer: allLinedStrings[i+3]];
[metaphors addObject:newMetaphor];
}
As for the problem, when I access any item via index ([metaphors objectAtIndex:3] for example) every other element (odd numbered ones) are nil elements. All of the objects are added to the array, though. My guess is that addObject is adding an element to the array as well as a new nil sentinel every time? Should this be happening/should I manually go through and remove these elements?
Also a side note, as I'm new to Objective-C, my Metaphor class contains the 4 instance fields you can see within the body: I'm sure there is quicker syntax to initialize one of these objects if anyone could point me the right way. Thanks.
NSArray can't contain nil. It's invalid. If you are getting nil back in a call to an array, the array pointer itself is almost certainly nil. (You CAN send messages to a nil object pointer in Objective C. It simply returns nil/zero.)
Trying to add a nil to an NSArray will cause a crash, and trying to index past the end of an NSArray will also crash.
There is a special class NSNull that provides a singleton placeholder object that can take the place of a nil entry in an NSArray.
The nil sentinel is only a way to know the last element of a variable length array has been reached. The nil sentinel ISN'T added to the NSMutableArray;
NSMutableArray addObject method doesn't need a nil sentinel as you only add ONE object, not a variable length array.
If you need to add something "nil" to an array, you might use [NSNull null] which is an object "equivalent to nil".

Exception in button tag

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.

Resources