NSdata length crash on device - iphone

While running this code:
NSData *archivedSavedData = [[NSData alloc] init];
archivedSavedData = [defaults objectForKey:#"listOfAccessNumbers"];
NSLog(#"archivedSavedData length is %d", [archivedSavedData length] );
I am getting this crash error (last line) only when running on a device that is connected:
[__NSCFArray length]: unrecognized selector sent to instance 0x2398a0
2012-03-13 20:25:33.088[7301:707] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0x2398a0'
* First throw call stack:
(0x34dbc88f 0x361e3259 0x34dbfa9b 0x34dbe915 0x34d19650 0xccb1b 0x31e13e33 0x31e38629 0x31dfcd7d 0x31ebf4dd 0x31e0555d 0x31e05579 0x31e0540b 0x31e053e7 0xcfedf 0x31e12e53 0x31e0c985 0x31ddac6b 0x31dda70f 0x31dda0e3 0x3600f22b 0x34d90523 0x34d904c5 0x34d8f313 0x34d124a5 0x34d1236d 0x31e0ba13 0x31e08e7d 0xcfd39 0xcbe28)
terminate called throwing an exception
This doesn't happen when running on the simulator or directly on the device with a distribution profile (through testflight for example).
Does anyone know how such a behavior could happen only in this case?
Thanks.
UPDATE: when trying to replace length with count I get this complication error: "No visible #interface for 'NSData' declares the selector 'count'"
UPDATE2: I understand that it should be an NSArray rather than an NSData, but my problem is that I did store archived NSData cause my array consists of custom objects, so I had to archived this data into NSData format when saving in NSUserDefault. How else should I approach that otherwise?
Thats how I store the data:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
[defaults setObject:data forKey:#"listOfAccessNumbers"];
array is an array of custom objects of the form of:
#interface NumberDataObj : NSObject {
NSString *inputName;
NSString *inputNum;
}

The error message says:
-[__NSCFArray length]: unrecognized selector sent to instance
That means that archivedSavedData is an array and that it doesn't (obviously) respond to length so you should declare archivedSavedData as an array and use count instead.
NSArray *archivedSavedData = [defaults objectForKey:#"listOfAccessNumbers"];
NSLog(#"archivedSavedData length is %d", [archivedSavedData count]);
Now, as to why this doesn't happen when running on the simulator, my guess is that your test scenarios don't make this part of the code get called.
EDIT
If you want to retrieve the data as NSData then use the method dataForKey:
NSData *archivedSavedData = [defaults dataForKey:#"listOfAccessNumbers"];
NSLog(#"archivedSavedData length is %d", [archivedSavedData length]);
The documentation says for dataForKey:
Return Value
The data object associated with the specified key, or nil if the key does not exist or its value is not an NSData object.
and for arrayForkey:
Return Value
The array associated with the specified key, or nil if the key does not exist or its value is not an NSArray object.
So aways use the appropriate method when you know the type of the data to avoid problems like this.

You have two problems as the code is written:
1) You allocate a variable called archivedSavedData that you reassign on the following line without releasing. This is okay if you are working with ARC, but the first line would then be unnecessary.
2) The second problem is that the object corresponding to the key #"listOfAccessNumbers" stored in your defaults object is of type NSArray. NSArray responds to the selector count, not length. Maybe you should look more closely at this object and recode accordingly.
Hope this helps :)

It's because [defaults objectForKey:#"listOfAccessNumbers"] returns an NSArray, not an NSData object.

Related

Unrecognized selector error processing results from geocodeAddressString

I'm trying to create multiple placemarks using MKMapItem without using coordinates.
I used location name directly in geocodeAdressString:#"Mumbai"... but I got result for single location.
While I use multiple locations through array, I'm getting this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0xab48380'
Why is this problem occurring?
Class mapItemClass=[MKMapItem class];
if(mapItemClass &&[mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
{
NSArray *addr=[[NSArray alloc ]initWithObjects:#"Banglore",#"Mumbai",#"Delhi", nil];
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *geocodedPlacemark=[placemarks objectAtIndex:0];
MKPlacemark *placemark=[[MKPlacemark alloc]initWithCoordinate:geocodedPlacemark.location.coordinate addressDictionary:geocodedPlacemark.addressDictionary];
MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
[MKMapItem openMapsWithItems:#[mapItem] launchOptions:nil];
}];
}
The error state that -[__NSArrayI length]: unrecognized selector sent to instance 0xab48380'
NSArray dont have a property length. So it is unable to find the selector of length. So check where you are using NSArray and keep break points to find where error is happening. length is the method of NSString and NSData but NSArray dont have length, it has count
I have been able to replicate this using this code
id array = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSLog(#"%d",[array length]);
output
-[__NSArrayI length]: unrecognized selector sent to instance 0x96bafe0
notice how I have used id instead of NSArray with using id I am able to call length which isn't allowed by NSArray, but this gets round the compiler when using id.
So the best way to find out where this is going wrong is add an exception that will catch all exceptions. Do this by selecting the exceptions tab in the project navigator wind >> select '+' >> 'Add Exception Breakpoint...' >> then just select done. This will set a breakpoint every time your app throws an exception.
EDIT
Thanks to the code you have added I suspect that you are passing an NSArray where there should be an NSString. You create
NSArray *addr=[[NSArray alloc ]initWithObjects:#"Banglore",#"Mumbai",#"Delhi", nil];
then pass it to geocodeAddressString:addr
[geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
Just from the name of this parameter I suspect it should be an NSString and not an NSArray try replacing addr to [addr objectAtIndex:0] this will get the string object at index 0 of the addr array.
EDIT 2
Here is the method you are calling notice it only allows an NSString to be passed in for geocodeAddressString.
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

CoreData expects a string when returning an array and NSData when returning a string

I have a simple NSValueTransformer that should convert a comma-separated string first, second, third into an array. So in my CoreData schema I have the attribute options which I set to Transformed and specify my Transformer.
This is my Transformer:
-(id)transformedValue:(id)value{
// convert it to an array
if(!value){
return nil;
} else{
NSString *languages = value;
NSArray *result = [languages componentsSeparatedByString: #", "];
// return result;
return #"test Result";
}
}
Now when I return the variable result I get the following error (length looks like it's expecting an NSString):
... 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x6cc59a0'
If I return the test string above instead I get this error (bytes looks like it's expecting an NSData object):
... 'NSInvalidArgumentException', reason: '-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0xf0780'
It looks like there is a conceptual problem which I don't seem to grasp. Also curiously +(Class)transformedValueClass and +(BOOL)allowsReverseTransformation are never "touched" when using the Debugger.
There are no errors in code you provide. The problem lays in your CoreData approach.
When you use transformed property in CoreData, the value is storing in database as NSData object. And length in your errors is related to NSData length method.
Therefore you should provide NSValueTransformer with transformation from NSData to NSArray and reverse to put concatenated arrays to CoreData as NSData.
If you want to keep the possibility to store property as NSString, you can implement special accessor to NSString property of your managed object, which will transform the NSString to NSArray value or use your NSValueTransformer outside your CoreData model like this:
NSValueTransformer *transformer = [NSValueTransformer valueTransformerForName:#"StringToArrayValueTransformer"];
NSArray *array = [transformer transformedValue:yourEntityItem.yourStringProperty];
The value needs to be cast to a NSString try this
-(id)transformedValue:(id)value{
// convert it to an array
if(!value){
return nil;
} else{
NSString *languages = (NSString*)value;
NSArray *result = [languages componentsSeparatedByString: #", "];
return result;
}
}

Initializing NSMutableDictionary to create a filter

This is a continuation question I had from a post yesterday: Initializing NSMutableDictionary
(It's probably a stupid question, but I don't see what I'm doing wrong.)
I liked the idea of just having "All" listed in my TableView that pops up when the filter button is pressed in order to indicate no filter applied. I get my data from my model class sorted correctly into an NSMutableArray like so:
NSArray *anArray = [dmgr.CategoryDictionary allKeys];
self.CategoriesArray = [anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
[self.CategoriesArray insertObject:#"All" atIndex:0]; // line causes crash
But I want to add the "All" option to the CategoriesArray to show in the UITableView. When I try to run it, it crashes at that line because of
NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x59baba0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x59baba0'
Any help would be appreciated. Thanks!
sortedArrayUsingSelector: returns an immutable array which is why you can't add to it. Do this to get a mutable copy.
self.CategoriesArray = [NSMutableArray arrayWithArray:[anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)]];
It seems CategoriesArray is actually a NSArray instance. From Apple's doc :
-(NSArray *)sortedArrayUsingSelector:(SEL)comparator;

-[__NSArrayI stringByReplacingOccurrencesOfRegex:withString:]: unrecognized selector sent to instance

Im trying to clean up a json stream and get the error listed above
NSString *traveladvice = [json valueForKeyPath:#"travel_advice_article.travel_advice_sections.body.markup"];
which gets me a stream with lots of javascript regular expressions like \U00a0 which I want to remove. Im using to regexlite to remove.
NSString *regexString = #"U00a0";
NSString *replacementString = #"";
NSString *travelparse1 = nil;
travelparse1 = [travelAdvice stringByReplacingOccurrencesOfRegex:regexString
withString:replacementString];
I understand the error but whats confusing me is that traveladvice is an NSString not an NSArray.
json valueForPath might return an array, not a string.
stop in a debugger and do 'po traveladvice'
it will show you the actual data.
also, store the result type as 'id' and not NSString* to avoid confusion

How to retrieve stored reference to an NSManagedObject subclass?

I have a NSManagedObject subclass named Tour. I stored the reference to it using this code:
prefs = [NSUserDefaults standardUserDefaults];
NSURL *myURL = [[myTour objectID] URIRepresentation];
NSData *uriData = [NSKeyedArchiver archivedDataWithRootObject:myURL];
[prefs setObject:uriData forKey:#"tour"];
Now I want to retrieve it. I tried using:
NSData *myData = [prefs objectForKey:#"tour"];
NSURL *myURL = [NSKeyedUnarchiver unarchiveObjectWithData:myData];
TourAppDelegate *appDelegate = (TourAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectID *myID = [appDelegate.persistentStoreCoordinator managedObjectIDForURIRepresentation:myURL];
if (myID)
{
Tour *tempObject = [appDelegate.managedObjectContext objectWithID:myID]; //WARNING
tour = tempObject;
}
if (tour) //instruction...
But it's giving me this warning "Incompatible Objective-c types. Initializing 'struct NSManagedObject *', expected 'struct Tour *'
Plus, when executing, it's giving me this: Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x5001eb0
How can I solve this?
Regarding the warning, did you try to force type casting?
Tour *tempObject = (Tour *) [appDelegate.managedObjectContext objectWithID:myID];
The problem related to NSObjectInaccessibleException is solved in the link St3fan posted :)
PS: Remember that a subclass of nsmanagedobject is still a nsmanagedobject!
This is a great article about storing and retrieving references to objects.
http://cocoawithlove.com/2008/08/safely-fetching-nsmanagedobject-by-uri.html
Looks like the URI you have is not registered in the context.
From the docs:
If the object is not registered in the context, it may be fetched or returned as a fault. This method always returns an object. The data in the persistent store represented by objectID is assumed to exist—if it does not, the returned object throws an exception when you access any property (that is, when the fault is fired). The benefit of this behavior is that it allows you to create and use faults, then create the underlying rows later or in a separate context.
objectRegisteredForID: will return nil if you want to gracefully fail from this condition