Converting File Path From NSString To NSURL - objective-c

I'm working through Cocoa smoothly, but this problem seems so basic it cancels out all the cool stuff I learned. :/
I have a generated file path, and it needs to be in NSURL format. From research, this is the code I wrote:
NSLog(#"Old path = %#", pathToFile);
NSURL *xmlURL = [[[NSURL alloc] init] fileURLWithPath:pathToFile];
NSLog(#"New path = %#", [xmlURL absoluteString]);
And the output:
2010-01-27 15:39:22.105 MusicLibraryStats[28574:a0f] Old path = file://localhost/Users/[username]/Music/iTunes/iTunes%20Music%20Library.xml
2010-01-27 15:39:22.105 MusicLibraryStats[28574:a0f] New path = (null)
First off, the alloc-init shouldn't even be necessary; other people seem to get away with it. In this case, if I don't alloc-init, I get an 'unrecognized selector' error on that line. Of course, now I'm just getting plain old (null).
Where did I goof?
Thanks!

The [[NSURL alloc] init] is not just unnecessary, it's invalid. fileURLWithPath: is a class method, which means you can only call it on the class object (that is, NSURL itself). It does not produce a compile error because -(NSURL *)init returns an object of type id, and does not result in a runtime error because -(NSURL *)init actually returns nil, and messages sent to nil will just cascade another nil as their return value.
This code should work:
NSString* pathToFile = #"/this/is/a/path";
NSURL* url = [NSURL fileURLWithPath:pathToFile];
I found your problem.
-[NSOpenPanel URLs] returns an array of NSURL objects, which you treat as NSString objects. That's not right. You should use the following:
NSURL* url = [[oPanel URLs] objectAtIndex:0];
The debugger could've show you that if you looked at the pathToFile variable. Make sure to check it next time. :) Hovering a variable with your mouse should get you its type.
However, remember that there are situations where you will legitimately encounter another type than the one you expected. For instance, the private NSPathStore2 class is part of the NSString cluster, and you can do everything NSString supports on NSPathStore2 objects. (If this happens and you're not too sure, check the documentation to see if the type you expect is a cluster type. That's how they're called in the documentation.)

Related

OSX App Unrecognized Selector error when downloaded image from a when downloaded image from a JSON web service

In my Mac app I am getting Data from hey JSON web service and one of the fields is an image. When I do a NSLog(#"%#", [dict valueForKeyPath:#"profileImageDataString"] ); I see all of the data and it is correct. And I can also put it in a NSURL object like this NSURL *imgUrl = [dict valueForKeyPath:#"profileImageDataString"]; and it is still correct. But when I try this NSData *imgData = [NSData dataWithContentsOfURL:imgUrl]; or this NSImage *userImage = [[NSImage alloc] initWithContentsOfURL:imgUrl];I get the error unrecognized selector sent to instance. If I put the data in a NSString, two thirds is cut off at the end.
I am trying to save the Image data from the Jason web service into a text file to that later it can be loaded into a NSImage on in a user settings view.
Someone tell me what it is that I'm doing wrong?
This is the value of profileImageDataString that is returned by the web service;
iVBORw0KGgoAAAANSUhEUgAAATQAAAE0CAYAAACigc+fAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAACC6SURBVHhe7Z37t1VV+cb7W/SIgCYgl8FNDOIWEAevEJY3aKSgiYNogGCCCYRiIFIMLiomKKAChZ7KAjMC5ZKEIYQgcbPM5KqAtzHmt2f6XccNvOw9995zrTXfuZ4fPuM5A845a+35zvc9z1rz9rV//etfJkT+/e9/U5Ur0c/777+vSm1BwxfUbJTEg2uSUbPTYBwabogahxL9uBSPEJUOLSMl8eCaXNTsNXeHhhuh6lYSDy5FI2SlQ0tZiX5ck4mav+bm0HADVN1K4sGlWGhQOrSUlOjHNYmo4WjmDg0XpupWoh+X4qBR6dA8K9GPa/JQw9PMHBouSNWtRD8uRUGz0qHVqSQeXJOGGq6m5tBwAWocSvTjUgxiUDq0GpXEg2uyUMPVBO8ODReg6lYSD1Lyx6x0aFUq0Y9rclDD13Px5tBwAapuJfGA5C6i0qE5KtGPa1JQw9cLUbdDwwWoupXoB8lMpUOrqEQ/rslADVddqdqh4QLUOJToB0lM/Urp0M5REg+uSUANV6vF2aHhAtQ4lOgHyUs9X+nQ/l9JPLh2fmq4WisVHRouQNWtJB6QtNQLa+EdGtGPa2enhq/1ckGHhgtQdSuJByQrtbIW1qER/bh2cmr46ovzHBouQNWtRD9ITmr1WjiHRvTj2rmp4WpafA0XcC0G1LCV6AdJSa1do3doJB5cOzU1XE0bOrQIlOgHyUitX6N1aCQeXDszNVzNCjo0hZo3uI/t27ebpqYms2DBAvPAAw+YkSNHmuuuu84MGDDA9O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z8MMP2761YsUK+xoE/Q4FDI+T+BpODNMv0DfpxmSicGhpkjRUaYGDe0t2+cSOBfjLiU6HmdgYYUInJNWBx6ciKUBfQZ9B34HrQl9Cn0Lfwot99DWpgCX9MlTNEzq0GkgaD50sAZ0uLVBIqbq1HPiehNI+VZqoxA3VDo3oB52QGoeGAB1awCBAVN1KskWlQyP6Qeej6tYQoUMLCASEGoeSfFDh0Eg8oNNRdWvI0KEFAAJBjUNJvgTt0Eg8oLNRdasG6NByBAGg6lYSFkE6NKIfdC5qHKoJOrQcQMNTdSsJk6AcGtEPOhU1DtUIHVqGoMGpupWETRAOjegHnYmqW2OADi1F0MDUOJToIBeHRuIBnYiqW2OCDi0F0LDUOJToIlOHRuIBnYeqW2OEDs0jaFCqbiW6ycShEf2gs1Dj0JihQ/MAGpKqW0kcpOrQiH7QSahxaBGgQ6sDNCBVtxL9JCdkQVNxaEQ/6CBU3VpE6NCqAA1GjUOJfkqdWaJeHBqJB3QMqm4tMnRoDqChqHEo0Y/kzBKty6GReECHoOpWQodWFjQQVbeSeCjnzBKtyaER/aADUONQ8hV0aAJoGKpuJfHg4swSrcqhEf0g8NQ4lJwPHVoJaBCqbiX6cXFisn5g/g91hyU8vt0T+gAAAABJRU5ErkJggg==
The string seems to be a base64 encoded string representation of an image.
Try to create an NSData object from the string and the image from the data.
NSString *imgDataString = [dict objectForKey:#"profileImageDataString"];
NSData *imgData = [[NSData alloc] initWithBase64EncodedString:imgDataString options:0];
NSImage *userImage = [[NSImage alloc] initWithData:imgData];
And I can also put it in a NSURL object like this NSURL *imgUrl = [dict valueForKeyPath:#"profileImageDataString"]; and it is still correct.
You haven't got an NSURL object. You've got an NSString object. Declaring that your pointer refers to an NSURL and using the untyped getter valueForKeyPath: (or, even, much more correctly, objectForKey:) does not change the type of the object.
To convert a string to a URL use +[NSURL URLWithString:]. But what you're showing us isn't a URL. So why try to make it one at all?
But when I try this NSData *imgData = [NSData dataWithContentsOfURL:imgUrl]; or this NSImage *userImage = [[NSImage alloc] initWithContentsOfURL:imgUrl];I get the error unrecognized selector sent to instance.
That's because you're supplying an NSString, which does not implement the same selectors as an NSURL.
If I put the data in a NSString, two thirds is cut off at the end.
No it isn't; however when you log any object, all you get is enough information to help in debugging. Using NSLog is not intended to be a way to store the same amount of data in another form, or indeed to do anything strictly well defined other than help you to debug.
Get your string, use the approach as suggested by vadian.

How to check if NSURL contains NSString null?

I use the following code to get a NSString from a NSDictionary and then cast it into NSUrl:
NSURL * url = [NSURL URLWithString:[self.item objectForKey:#"website"]];
The NSDictionary self.item comes from a web server and it's correctly filled using JSON data. All the other objects inside the NSDictionary work perfectly fine.
But sometimes the web server passes a website url with the text "null" because the object has no website filled in. From debugging i learned that the NSURL object can contain a url with the text "null". But how do i prevent this, or how can i write an if statement that checks this?
I tried the following:
NSString * niks = [eventUrl absoluteString];
if(niks == #"null")
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Event" message:#"Event heeft geen website" delegate:nil cancelButtonTitle:#"Oke" otherButtonTitles:#"Oke", nil];
[message show];
}
else
{
[webView loadRequest:[NSURLRequest requestWithURL:eventUrl]];
NSLog(#"%#",eventUrl);
}
But this doesn't work, it always passes the url directly to the webview. Can someone set me in the right direction?
Your comparison;
if(niks == #"null")
only compares if the pointers are equal (i.e. if the two are the same string object instance). Since one is a constant and the other is created dynamically from JSON fetched from the server, it's very unlikely.
To compare the content of two strings, you should instead do;
if([niks isEqualToString:#"null"])
For the link thirsty, here is the [NSString isEqualToString:] documentation.
To do string comparison, you need to do [niks isEqualToString:#"null"], that's why the first condition is broken.
You can also use [niks RangeOfString:#"null"];
I believe you can do this:
id str = [self.item objectForKey:#"website"];
if([str isMemberOfClass[NSNull class]]) {
... its null
}
The JSON convertors all change null to a NSNull object (in my experience).
I am not 100% clear of my memory, but I encountered a situation where [NSDictionary objectForKey:] actually returned NSNull class instance instead of "null" string.
If this is the case, you can check the class of [self.item objectForKey:#"website"] by using isKindOfClass method.

Xcode keeps telling me my usage of NSDirectoryEnumerator is wrong.

I am trying to instantiate an NSDirectoryEnumerator from a NSFileManager like this
NSDirectoryEnumerator *enum = [fm enumeratorAtURL:*url includingPropertiesForKeys:nil options:nil errorHandler:nil];
and Xcode keeps coming with an error message that even after reading the complete FileManager and DirectoryManager Documentation doesn't make any sense to me:
Expected identifier or '('
Do I have to import anything else than Core Foundation to use NSDirectoryEnumerator or is there some special trick to the usage syntax that I am missing?
// edit:
url is of course a valid NSURL. Just in case this might come up.
In C and, by extension, Objective-C, enum is a keyword. Change the variable name.
Also, you need to remove the asterisk, unless it's a pointer to NSURL * (i.e. NSURL **) as commented by #WTP:
NSDirectoryEnumerator *enum = [fm enumeratorAtURL:*url includingPropertiesForKeys:nil options:0 errorHandler:nil];
^
Right here!
And the options parameters should be 0, not nil as noted by #omz.

Unknown error creating file in objective c

I'm creating a mac app that needs to create a file with the contents of another file, i'm creating it as follows:
NSString *p = #"/AfilethatEXISTS.plist";
NSString *user1 = #"~/Library/MyApp/myFile";
NSString *pT1 = [user1 stringByExpandingTildeInPath];
[[NSFileManager alloc] createFileAtPath:[NSURL URLWithString:pT1] contents:[NSData dataWithContentsOfFile:p] attributes:nil];
However returning no error, its not creating the file?
There are several things wrong with this code, but not enough context to tell you what is going wrong.
First, there should never be a file in / directly. That directory should be sacrosanct and many users will not be able to write to that directory without admin access.
Secondly, paths should be managed via the path manipulation APIs on NSString and NSURL.
Next, pT1 isn't really an URL and that is URLWithString: may be returning nil. Use fileURLWithPath: instead.
Finally, there isn't any error checking in that code and, thus, there is no way to tell how you might have discovered no error. What have you checked?
First off, you're creating the file manager instance incorrectly. To create a new instance, you need to both allocate and initialize it.
You're trying to pass an NSURL object, which won't be created correctly since the string you're using to create it with isn't a URL. But that doesn't matter anyway, because even if the NSURL was created, -createFileAtPath:contents:attributes: expects an NSString - just pass pT1 directly.
Better still, since you're basically just copying p to pT1, use the NSFileManager method for doing that. Not only is it conceptually a better fit, it also gives you a chance to examine a returned NSError object to see what (if anything) went wrong.
NSError *error;
NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
if (![fm copyFileAtPath:p toPath:pT1 error:&error]) {
// If copyFileAtPath:toPath:error: returned FALSE, an error occurred, and
// error will point to an NSError instance with more information
}

Objective-C: initWithContentsOfURL returning null

I'm working on an app that would display a remote html and use local images, so what I'm trying to do is download the HTML of the website and display it as a "local" page, that would have access to images in my bundle.
For some reason I can't get initWithContentsOfURL to work. I checked all manuals and examples I could find and it seems that I'm doing it correctly, but the thing just won't work, returns null all the time. The same page loaded with NSURLRequest requestWithURL works fine.
Here is the code:
- (void)awakeFromNib
{
appURL = #"http://dragontest.fantasy-fan.org";
notConnectedHTML = #"Could not connect.";
NSString *seedString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#/seed.php", appURL]]];
NSString *HTMLdata = #"";
if (seedString = #"(null)") {
NSLog(#"Can't connect on awakeFromNib.");
HTMLdata = notConnectedHTML;
}else {
HTMLdata = [NSString stringWithFormat:#"<body style='padding:0px;margin:0px;'>%#%#</body>", seedString, #"<br><img src='images/Default.png'>"];
}
[homeView loadHTMLString:HTMLdata baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
}
Firstly, why aren't appURL and notConnectedHTML declared as NSString *? Are they declared this way elsewhere?
Secondly, you might be better off using NSURL's -urlWithString:relativeToURL: to create the actual request URL.
Thirdly (and this is your actual problem here I suspect), to compare two C primitives, you use ==. = is the assignment operator (it makes the thing on the left equal to the thing on the right). To compare two Objective-C objects, use a comparison method, like -isEqual: or -isEqualToString: (which is specifically for NSStrings).
So instead of:
if (seedString = #"(null)")
You should use
if ([seedString isEqualToString:#"(null)"])
However I suspect the reason you're trying to compare to "(null)" is because that's what NSLog spits out when an object is equal to nil. When an object is nil, the object reference itself is equal to the nil constant, so you should use this to see if an object is nil:
if (seedString == nil)
Just for good measure, some people like to use this syntax which does exactly the same thing:
if (!seedString)