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

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]

Related

NSURLThumbnailDictionaryKey empty for local file

I want to get a thumbnail representation of a file I have to display in my app. I'm using NSURL here:
NSDictionary *thumbnails = nil;
BOOL success = [fileURL getResourceValue:&thumbnails
forKey:NSURLThumbnailDictionaryKey
error: &error];
This works fine if I am connected to iCloud, and the URL is a link to a file stored in iCloud. The fileURL is something like:
file:///Users/me/Library/Mobile%20Documents/BJXXGLR9R3~com~myapp~icloud/FileStorage/contact-page%20copy.png
If I use the same code with a NSURL pointing to a local file, however, the thumbnails dictionary is empty.
Here is an example of the URL in this case:
file:///Users/me/Library/Containers/com.mycompany.mymacapp/Data/Library/Application%20Support/com.mycompany.mymacapp/FileStorage/Bn4VaCnCUAEJjLb.png-large.png
Is this API for getResourceValue not supposed to work with locally stored files? Or am I doing something wrong?
This is part of the API. Furthermore you should use a File coordinator while working with files coming from iCloud:
[url startAccessingSecurityScopedResource];
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
__block NSError *error;
[coordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
[newURL getResourceValue:&image forKey:NSURLThumbnailDictionaryKey error:&error];
}];
[url stopAccessingSecurityScopedResource];

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

Ignoring SSL certificate errors with NSURLConnection

I'm using [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)] to pull data from a web service, but the web server has a self-issued certificate, causing this error to appear:
Error displayed using:
NSAlert *a = [NSAlert alertWithError:error];
[a runModal];
Is there any way to ignore this error and continue anyway?
Following the instructions in the linked question, I defined a dummy interface for NSURLConnection:
#interface NSURLRequest (DummyInterface)
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
#end
And called the method before creating the request:
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
This suppressed the error about the certificate being invalid.
This may be rejected by Apple. Use the proper implementation of NSConnectiondatadelegate:
see a way around it:
Stackoverflow response to similar question

What is the encoding for URL bookmarks stored as NSData?

What is the best way to get the path from the NSData bookmark object, if the bookmark will not resolve?
Normally, you just resolve the bookmark, you get a URL, and off you go. But if the bookmark is to an NFS mount that is not currently present, it won't resolve. So now I have an NSData pointing somewhere that won't resolve, but I don't know where it points.
Here is the code block I have that loads the bookmarks, tries to resolve them, and attempts to decode the NSData if the resolve fails, but I can't figure out the encoding - is this even possible?
NSError* error = [[NSError alloc] init];
NSURL* resolvedURL = [NSURL URLByResolvingBookmarkData:bookmarkData
options:NSURLBookmarkResolutionWithSecurityScope | NSURLBookmarkResolutionWithoutUI
relativeToURL:nil
bookmarkDataIsStale:NULL
error:&error];
if (resolvedURL) {
// do some stuff
...
} else {
NSString* msg = [NSString stringWithFormat:#"Error Resolving Bookmark: %#", error];
NSLog(msg);
// the below certainly doesn't get me a path from the bookmark, any idea what will?
// NSString* path = [[NSString alloc] initWithData:bookmarkData encoding:NSUTF32StringEncoding];
}
I never did figure out the encoding, but I found a workaround.
Originally, I encoded the sandboxed NSURLs into NSData objects, and then stored those as an NSArray in NSDefaults. Therefore, I had no way to determine the path for the NSData, unless it would resolve.
The workaround was to change the design - now I encode the sandboxed NSURL, store it as an object into an NSDictionary with the key being the URL path, and store the NSDictionary in NSDefaults.
With this approach, I can easily retrieve the NSData for any given path, even if it will not resolve.

What do I do with NSData?

I am trying to send email programmatically... I have this line of code:
_mail.AddAttachmentData(nsd,"text/plain", filePath);
and I haven't a clue what goes into NSData... I tried taking the string that I was writing to a file, but apparently that doesn't work either. I believe it is preventing me from doing a good sendmail.
NSData is a class type. See here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
You'll probably want to construct your NSData object using something like this:
NSString *myFilePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSData *myData = [NSData dataWithContentsOfFile:myFilePath];
Then pass that in as the NSData object.

Resources