UIAddressBook code puts error on device - iphone

I testing following code on simulator ,it works fine but when I select the device and run then it gives exception .
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
tempPeoples= [NSMutableArray arrayWithCapacity:0];
for(int i=0;i<nPeople;i++){
ABRecordRef i1=CFArrayGetValueAtIndex(allPeople, i);
NSString* name = (NSString *)ABRecordCopyValue(i1,kABPersonFirstNameProperty);
[tempPeoples addObject:name];
// [peoples addObject:i1];
}// end of
following exception occurs
2011-01-06 12:12:42.384 Appointment[2849:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
2011-01-06 12:12:42.397 Appointment[2849:207] Stack: (
843263261,
825818644,
842812211,
842812115
Please help

You're adding a nil to an array (as the message says)
By a process of deduction, I see that you are adding objects to an array at this line
[tempPeoples addObject:name];
So it is likely that, for this snippet of code, this is where the error happens.
Probably, Not all of the Contacts have a first name, which is likely to be the case for contacts that are businesses rather than people.
You could put a breakpoint in the code and run it through a debugger to see what conditions cause this.

Related

How to remove bytes from the end of NSMutableData object

I have a NSMutableData object that is giving me some trouble, I am trying to remove the last 6 bytes from the object like this
NSMutableData *reducedDataPacket = [[NSMutableData alloc] init];
reducedDataPacket = [myCompressedData copy];
NSRange range = NSMakeRange([reducedDataPacket length]-6, 6);
[reducedDataPacket replaceBytesInRange:range withBytes:NULL length:0];
However once the last line executes my app crashes and I am left with this error below.
-[NSConcreteData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x1f037870
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x1f037870
I have never tried doing this before and have been going off other answeres supplied I have investigated, but I just cannot get this to work... any help would be greatly appreciated.
Your first line is useless because you then redefine reducedDataPacket in the second line, so that first line should be deleted. I'm guessing that myCompressedData is NSData rather than NSMutableData, so change that second line to :
NSMutableData *reducedDataPacket = [myCompressedData mutableCopy];
First you need a mutable instance, it isn't clear why you create one and then copy it. You should just do:
NSMutableData *reducedDataPacket = [myCompressedData mutableCopy];
Then you want to reduce the length, not try to fill part of the data with nothing:
[reducedDataPacket setLength:(reducedDataPacket.length - 6)];

Putting objects from one NSMutableArray to another NSMutableArray

I have 2 NSMutableArrays and i want to put certain object form 1 array to the other array I have already code written but it doesnt work and it gives me this error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 296 beyond bounds [0 .. 295]'
* First throw call stack:
(0x1c9a012 0x10d7e7e 0x1c3c0b4 0x2f04 0xb20b90 0x1c59376 0x1c58e06 0x1c40a82 0x1c3ff44 0x1c3fe1b 0x1bf47e3 0x1bf4668 0x1f65c 0x252d 0x2455)
libc++abi.dylib: terminate called throwing an exception
Initialisierung in viewdidload:
arrayLine1 =[[NSMutableArray alloc] initWithCapacity:80000];
arrayLine1a =[[NSMutableArray alloc] initWithCapacity:70000];
line1tagzahl=0;
line1tagzahl2=0;
passing code:
for (int a=0; a<10; ) {
[arrayLine1a insertObject:[arrayLine1 objectAtIndex:line1tagzahl2] atIndex:line1tagzahl2];
line1tagzahl2=line1tagzahl2+1;
a=a+1;
}
function to create objects in array(this function is called very fast and frequently) :
for (float a=0; a<0.8; ) {
UIImageView *line1 =[[UIImageView alloc] initWithFrame:CGRectMake(Startpoint1.center.x-(w/2),Startpoint1.center.y-kurve1yf+a,w,h)];
line1.image=[UIImage imageNamed:#"Unbenannt"];
line1.tag=line1tagzahl;
[self.view addSubview:line1];
[arrayLine1 insertObject:line1 atIndex:line1tagzahl];
line1tagzahl=line1tagzahl+1;
a=a+0.1;
}
now you should have more information
and if you ask i have more than 10 objects in array2
Seems you haven't read the error message you posted. You have 296 objects in the array. But if you just want to copy the array, why don't you... er... copy it?
NSMutableArray *secondArray = [firstArray mutableCopy];
Is there any problem with using a default method -addObjectsFromArray: of NSMutableArray class?
- (void)serverGotResponse:(NSArray *)objects {
[_myMutableArray addObjectsFromArray:objects];
}
To prevent out of bounds exception, you should use count property:
while (i < array.count) {
// do something ..
i++;
}
if you are copying all the objects, you can just copy the entire array:
array2 = [array1 copy]
You are trying to add the first 10 objects, but your exception was thrown on index 296, which is the first index that comes out of bounds.
For some reason, your while condition is not working properly, so I suggest you start looking there
I also suggest you use a for, it's actually more simple and straightforward than a potential infinite loop
for (int a = 0; a < 10; a++) {
[array1 insertObject:[array2 objectAtIndex:a] atIndex:a];
}

Extracting Data from NSMuableDictionary to NSString and compering to String issue

Im Extracting Data from NSMuableDictionary to NSString and try compering to String like this:
NSDictionary *error = [[NSDictionary alloc]init];
NSString *errorCode = [[NSString alloc]init];
error = [sing.globalCallsDitionary valueForKey:#"Error"];
NSLog(#"error code %#",[error valueForKey:#"error_code"]);
errorCode = [error valueForKey:#"error_code"];
if ([errorCode isEqualToString:#"-1"]) {
When the if statement executed i get this error talking about an array, the error looks like this:
2012-04-27 06:34:49.686 CallBiz[10319:707] error code (
"-1"
)
2012-04-27 06:35:00.602 CallBiz[10319:707] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16fc10
2012-04-27 06:35:00.608 CallBiz[10319:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16fc10'
*** First throw call stack:
(0x3541f88f 0x36846259 0x35422a9b 0x35421915 0x3537c650 0xb2a3 0x353793fd 0x32458faf 0x32458f6b 0x32458f49 0x32458cb9 0x324595f1 0x32457ad3 0x324574c1 0x3243d83d 0x3243d0e3 0x3667222b 0x353f3523 0x353f34c5 0x353f2313 0x353754a5 0x3537536d 0x36671439 0x3246be7d 0x2e61 0x28fc)
terminate called throwing an exception
It is as xCode is looking on my NSString *errorCode = [[NSString alloc]init]; as if it is an NSArray, can someone help me with this?
Error occurs because your errorCode came as a array, instead of string. If you are sure that only 1 object come here, then you can use -
if ([[errorCode objectAtIndex:0] isEqualToString:#"-1"])
It will work, although it will through a warning, because errorCode is defined as a string object. So you ned to sure what data you are getting in response and then define data structure appropriately.

NSFastEnumerationMutationHandler crash

Why am I getting NSFastEnumerationMutationHandler crash all of a sudden in my code. I am blank why this crash popped up all of a sudden and how to squash it.
Crash Error:
**** Terminating app due to uncaught exception 'NSGenericException', reason: '* **Collection <__NSArrayM: 0x610000859410> was mutated while being enumerated.'*
You must be trying to change an array while you using fast enumeration.
Example
for ( id anObject in anArray ) {
if ( /* anObject satisfies some condition */ ) {
[anArray removeObject:anObject];
}
}
That shouldn't be done. Use a different array or probably filteredArrayUsingPredicate: method to filter. Remedy, however, depends on what you're trying to do.
Came here looking for a solution and ended up taking a copy of the original array to get around the issue.
for (NSObject *object in [array copy]) {
if(condition) {
[array removeObject....]
break;
}
}

Search string in NSMutableArray for showing in Table

Okay, I have an Book Object Class. That has Book.name and Book.id as extensions.
Book.name being NSString. Which is what should be searchable and listed in UITableView.
NSMutableArray *matchingSymptomsArray = [[NSMutableArray alloc] initWithCapacity:50];
for(NSMutableArray *letterArray in DataArray){
for(Book *bk in letterArray){ //Heres the ERROR
NSLog(#"Uptil NEW");
if([bk matchesSearchString:searchString]){
//I couldnt reach here, according to debugging and Logs//
[matchingSymptomsArray addObject:bk];
}
}
}
DataArray is where all the Books are held.. more than a hundred. Its the main datasource for the UITableView. I'm trying to search and match each letter of the string with the Book.name, but even before I can begin, I cant do the
for(Book *bk in LetterArray) //producing the error..
What seems to be the problem?
This is the error in console
**** -[Book countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x55c110
2009-09-29 06:17:41.073 Smart Diagnosis[3897:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[Book countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x55c110'
Any work arounds??
thanks for your help :)
Try this:
for (id bk in letterArray) { }
Did you check that letterArray isnt nil or empty?
Did you check that letterArray isnt nil or empty?