Mac ABRecord setValue forProperty error - macos

When I use AddressBook like this :
ABAddressBook *ab = [ABAddressBook addressBook];
ABPerson *person = [[ABPerson alloc]init];
[person setValue:lastName forProperty:kABLastNameProperty];
[ab addRecord:person];
[ab save];
I got error :
`Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x1001448a0'`
What does setValue:forProperty do

It looks like the problem is in
-[__NSCFString count]
The setValue:forProperty sets value for a property. It looks like the problem might be your lastName object that you are trying to set.

Related

Using FaceBook SDK why do I get 'NSInvalidArgumentException', reason: '-[__NSDictionaryM length]: unrecognized selector sent to instance

This code gives this error...why ?
'NSInvalidArgumentException', reason: '-[__NSDictionaryM length]: unrecognized selector sent to instance
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithObjectsAndKeys:longitude,#"longitude",latitude,#"latitude",nil];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: info, #"venue", nil];
NSString *graphPath = [NSString stringWithFormat:#"/%#", (ID_OF_EVENT)];
FBRequest *updateEvent = [FBRequest requestWithGraphPath:graphPath parameters:params HTTPMethod:POST];
[updateEvent startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
I am trying to set the venue longitude and latitude, but no matter what I try I keep getting the run time error. What am I missing ? I thought "venue" is a dictionary under the main event and has the valid keys "longitude" and "latitude". Have one my parameters got to be an Array and not a dictionary ?
Actually I suspect these elements are non writable and I probably have to add the address at #"location" to see the details appear.
Regards

How to tell RestKit which mapping to use when using sendObject

I'm new to RestKit so this question may be dumb: When using [objectManager sendObject:...], how do I tell RestKit which mapping it should use for the result? More specific: I am sending GET and POST data to a server which in return responds with a JSON encoded message:
{"purchaseresult":{"status":"ok","errormsg":""}}
The Objective-C code I use looks like this:
RKObjectMapping *purchaseResultMapping = [RKObjectMapping mappingForClass:[PurchaseResult class]];
[purchaseResultMapping mapKeyPathsToAttributes:#"status", #"status", #"errormsg", #"errorMessage",nil];
[objectManager.mappingProvider setMapping:purchaseResultMapping forKeyPath:#"purchaseresult"];
[[RKObjectManager sharedManager].mappingProvider setErrorMapping:purchaseResultMapping];
[objectManager sendObject:queryParams toResourcePath:#"/purchase/post" usingBlock:^(RKObjectLoader* loader) {
loader.method = RKRequestMethodPOST;
loader.resourcePath = #"/purchase/post";
loader.params = queryParams;
loader.objectMapping = purchaseResultMapping;
}];
This returns an RestKit error:
restkit.network:RKObjectLoader.m:216 Encountered errors during mapping: Expected an object mapping for class of type '__NSDictionaryI', provider returned one for 'PurchaseResult'
Any ideas what I am doing wrong here?
Thanks
Christian
The object that you're sending is a NSDictionary so RestKit is looking for an object mapping for an NSDictionary but you are providing a PurchaseResult mapping. If you send an instance of PurchaseResult or provide your mapping for the NSDictionary class then this error should be avoided.
An alternative to this would be as follows:
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:#"/purchase/post" usingBlock:^(RKObjectLoader *loader) {
loader.delegate = **Your loader delegate here**;
loader.method = RKRequestMethodPOST;
loader.params = queryParams;
}];
Just make sure you've defined your mapping for the objects coming back in and you should be good.

Xcode: getting badge value to add it to a label

I have an app that receives push notification.
I would like to add the badge with the right value near a button inside the application menu (do you remember the old Facebook app?).
I'm trying to get the badge value from the notification in the AppDelegate, save it in NSUserDefault to use it in other view controllers.
NSString * badgeValue = [[userInfo valueForKey:#"aps"] valueForKey:#"badge"];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:badgeValue forKey:#"badge"];
[defaults synchronize];
The problem is that if I try to put the value in a label the app crashes
'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance
Xcode makes me save badgeValue in a string but when I put the string in the label it says is not a proper string
if ([badgeValue isKindOfClass:[NSString class]]) {
NSLog(#"it is string !!!!!!!!!!!!!!!");
// treat it as a string object
} else {
NSLog(#"it is not a string !!!!!!!!!!!!!!!");
// treat it as a number object
}
By debugging I see that badgeValue is not a real string but __NSCFNumber and if I try to convert it in a string the return value is a long strange number.
I searched everywhere for a way to get the badge value of the notification but the only option I can think of is a php query...Any idea?
What do you mean by "Xcode makes me save badgeValue in a string"? Do you get an error or a compiler warning if you try to type badgeValue as an NSNumber? The documentation and your own logging tell you that the value of the key "badge" is an NSNumber.
NSNumber * badgeValue = [[userInfo valueForKey:#"aps"] valueForKey:#"badge"];
[label1 setIntValue:badgeNumber.intValue];
Does something like this not work?
Yes, Just yesterday I tried with
NSString *string = [NSString stringWithFormat:#"%d", [badgeValue intValue]];
And it works! Thanks for help

What does -[NSURL length]: unrecognized selector sent to instance 0x1001c0360 mean

I've been trying to get some example code interfaced with a Cocoa interface(It had been written using Carbon); however, when I attempted to replace
err = ExtAudioFileCreateNew(&inParentDirectory, inFileName, kAudioFileM4AType, inASBD, NULL, &fOutputAudioFile);
with
err = ExtAudioFileCreateWithURL(CFURLCreateWithString(NULL,(CFStringRef)inFileName,NULL),kAudioFileM4AType,inASBD, NULL,kAudioFileFlags_EraseFile, &fOutputAudioFile);
I started to get these exceptions
2011-09-25 10:27:31.701 tester[1120:a0f] -[NSURL length]: unrecognized
selector sent to instance 0x1001c0360
2011-09-25 10:27:31.701 tester[1120:a0f] -[NSURL length]: unrecognized selector sent to instance 0x1001c0360.
I've looked at several other questions and answers and in all of those cases the problem was related to a NSURL being passed when a NSString was expected; however, I can't find where/if I'm doing that. I've looked at the documentation and as far as I can tell with my extremely limited knowledge of Apple's APIs. I'm not doing anything wrong.
Any help would be greatly appreciated.
May be help you, i had same problem
I was trying to make UIImage from :
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]];
Then its solved with by making string with [NSString stringWithFormat:]
NSString *urlStr =[NSString stringWithFormat:#"%#", [_photosURLs objectAtIndex:indexPath.row]];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
The error message is pretty clear. NSURL class does not have a -length instance method.
Have you tried to create the NSURL object with Objective-C syntax and cast it to CFURLRef?
I had the same issue while getting url from string like
[NSString stringWithFormat:#"%#Activity/GetBudget/%#",self.baseURL,activityID]
and I resolved it by calling absoluteString
like this
[[NSString stringWithFormat:#"%#Activity/GetBudget/%#",self.baseURL,activityID] absoluteString]

NSManagedObject not saving

Apologies if this has been answered before but I can't find a reference. I am trying Cocoa / obj-c for the first time. I am trying to knock up an app which will sync with a remote backup system via http (a la s3) and am stumbling around some fundamental core data issues.
I have created an entity and can invoke this without issues. The problem arrives when I call save on NSManagedObjectContext.
I am not going to include all methods involved in invoking the object context / model as the log output should (I think) verify that it is working as expected.
Best described with code and appropriate log entries.
*First, for illustration, I am invoking the managed object: *
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
NSLog(#"The managed object model is defined as follows:\n%#", managedObjectModel);
return managedObjectModel;
}
And the log output from the above NSLog:
2011-09-06 14:31:38.322 TryAgain[18885:a0f] The managed object model is defined as follows:
(<NSManagedObjectModel: 0x2000e2b00>) isEditable 1, entities {
BackupItinerary = "(<NSEntityDescription: 0x20020e9e0>) name BackupItinerary, managedObjectClassName NSManagedObject, renamingIdentifier
BackupItinerary, isAbstract 0, superentity name (null), properties {\n \"file_url\" = \"(<NSAttributeDescription: 0x2000faec0>), name
file_url, isOptional 0, isTransient 0, entity BackupItinerary, renamingIdentifier file_url, validation predicates (\\n), warnings (\\n),
versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n \"last_sync_date\" =
\"(<NSAttributeDescription: 0x2000faf60>), name last_sync_date, isOptional 1, isTransient 0, entity BackupItinerary, renamingIdentifier
last_sync_date, validation predicates (\\n), warnings (\\n), versionHashModifier (null), attributeType 900 , attributeValueClassName
NSDate, defaultValue (null)\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null)";
}, fetch request templates {
}
This looks so have been successful. No exceptions thrown, or warnings.
Now, the actual problem arrives when I call save on the object context. I have a NSOpenPanel which allows for picking dir / files to backup (all hooked up and working fine). Upon the user selecting a dir/file I want to set the value, so I:
NSArray *paths = [panel URLs];
NSURL *filePath = [paths objectAtIndex:0];
[directories addObject:filePath];
[directories setArray:[[NSSet setWithArray: directories] allObjects]];
NSEntityDescription *BackupItineraryEntity = [[self.managedObjectModel entitiesByName] objectForKey:#"BackupItinerary"];
NSManagedObject* BackupItinerary = [[NSManagedObject alloc]
initWithEntity:BackupItineraryEntity
insertIntoManagedObjectContext:self.managedObjectContext];
[BackupItinerary setValue:[filePath absoluteString] forKey:#"file_url"];
NSLog(#"entity:\n%#", BackupItinerary);
And the call to NSLog says (having selected /Users/rick/selenium2-webdriver/):
2011-09-06 14:31:38.328 TryAgain[18885:a0f] entity:
<NSManagedObject: 0x200216c80> (entity: BackupItinerary; id: 0x200090860 <x-coredata:///BackupItinerary/t0C005B39-D185-454B-B364-31314EEB10F02> ;
data: {
"file_url" = "file://localhost/Users/rick/selenium2-webdriver/";
"last_sync_date" = nil;
})
So, the file_url seems to be populated then, yes? But when I:
NSError *error;
if (![[self managedObjectContext] save:&error]) {
NSLog(#"Unresolved error %#, %#, %#", error, [error userInfo],[error localizedDescription]);
}
The log says:
2011-09-06 14:31:38.330 TryAgain[18885:a0f] Unresolved error Error Domain=NSCocoaErrorDomain Code=1570 UserInfo=0x2000cc4e0 "file_url is a required
value.", {
NSLocalizedDescription = "file_url is a required value.";
NSValidationErrorKey = "file_url";
NSValidationErrorObject = "<NSManagedObject: 0x20020f660> (entity: BackupItinerary; id: 0x20008faa0 <x-coredata:///BackupItinerary/t0C005B39
D185-454B-B364-31314EEB10F03> ; data: {\n \"file_url\" = nil;\n \"last_sync_date\" = nil;\n})";
}, file_url is a required value.
So basically:
I can, it seems (?), invoke the an entity and set a value on it, but when it comes to saving it the values is not set. The above code is executed inline and I am using garbage collection.
Feels like a total newb school boy error issue but for the life of me I can't see what I am missing having gone over the docs, highlevel tutorials and example code.
Pointers appreciated!
You are looking at two different managed object instances. The instance that has its file_url property set is <NSManagedObject: 0x200216c80> while the one that reports the error is <NSManagedObject: 0x20020f660>.
Somewhere, you are inserting an instance but not giving it a file_url value. In the code given, it might happen if the user selects cancel in the NSOpenPanel.
Judging from the output of your NSLog's, I'd say that BackupItinerary object is different when saving your context. Check this output on different occasions:
When inserting and setting the value:
entity:
<NSManagedObject: 0x200216c80>
When saving:
NSValidationErrorObject = "<NSManagedObject: 0x20020f660>
As you can see the addresses of instances differ, hence you're inserting one, but saving the other instance. Hope this helps.

Resources