Error converting a NSString to a NSURL - macos

The following snippet produces the debugger listing that follows it. Yet, if Line Two replaces Line One, there is no error. LineTwo was obtained by coping and pasting the debugger output from a previous run. Can you explain this to me?
//LINE ONE
NSString* aString = self.urlText;
NSLog(#"aString: %#",aString);
//LINE TWO
//NSString* aString = #"file:///Users/oldmancoyote1/Documents/A%20REFERENCE%20SYSTEM/C/Computers/iOS/C%20and%20Obj.%20C/C%20Language/1-4.6.html";
NSURL* testURL = [NSURL fileURLWithPath:aString];
2016-01-03 12:44:30.307 webScrol[1505:62452] aString: file:///Users/oldmancoyote1/Documents/A%20REFERENCE%20SYSTEM/C/Computers/iOS/C%20and%20Obj.%20C/C%20Language/1-4.6.html
2016-01-03 12:44:30.307 webScrol[1505:62452] -[NSURL length]: unrecognized selector sent to instance 0x6080000a4800
2016-01-03 12:44:30.307 webScrol[1505:62452] Failed to set (contentViewController) user defined inspected property on (NSWindow): -[NSURL length]: unrecognized selector sent to instance 0x6080000a4800

aString is actually an NSURL object despite being stored in an NSString variable. This must have happened in earlier code than what you have provided.
When it’s passed to [NSURL fileURLWithPath:], that function tries to treat it as an NSString by calling its length method and the program crashes.

Related

get file attributes with nsfilesustem in cocoa no such file exists

I've searching for about 3 hours of how to get the creation date of a file, I get the URL with an enumerator, after that I pass it to path correcting the percents, and finally I try to get the file attributes...so, I don no alter the path in anyway but I always get the same error "The operation couldn’t be completed. No such file or directory", and the path "file:///Users/raul/Desktop/DSC_0386.JPG".
The code sample:
NSError* error = nil;
//NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:[[url absoluteString] stringByRemovingPercentEncoding] error:&error];
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:#"file://Users/raul/Desktop/DSC_0386.JPG" error:&error];
NSLog(#"%#",error);
NSDate *fecha = [fileAttribs objectForKey:NSFileCreationDate];
I've commented the first NSDictionary to try out the second statement with the nsstring directly.
I've checked that my file already exists.
Please, any help?? I'm missing anything?
Several issues:
1) In most cases, you shouldn't have to convert an NSURL to a path string in order to operate on a file. In particular, you can use the "resource value" API of NSURL to get the creation time directly:
NSDate* creationDate;
NSError* error;
if ([url getResourceValue:&creationDate forKey:NSURLCreationDateKey error:&error])
/* use creationDate */;
else
/* handle error */;
2) If you do need to get a path string from NSURL, don't use -absoluteString. That will still be a URL string, with things like "file://", etc. A URL string is not a valid path string. The error message you quoted in your question was already telling you this. It showed you a file "path" of "file:///Users/raul/Desktop/DSC_0386.JPG", but that's not a path at all.
You should just use the -path method. You do not need to do anything with percent encoding when you get the -path.
3) You should ignore any error output parameter until you have checked whether the method you called succeeded or failed, usually by examining its return value. That is, the code you posted should be reorganized like this:
NSError* error = nil;
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:#"file://Users/raul/Desktop/DSC_0386.JPG" error:&error];
if (fileAttribs)
{
NSDate *fecha = [fileAttribs objectForKey:NSFileCreationDate];
// ... use fecha ...
}
else
NSLog(#"%#",error);

Converting NSDate to NSString causes unrecognized selector exception

I am storing an NSDate in a plist as a string, and at launch I am trying to convert the string from the plist back to an NSDate to compare it later.
This is how I am storing the value in my plist:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
In the log (When I convert the [NSDate date] to a proper string) it says this:
2013-04-13 22:47:57 +0000
This is how I am trying to convert the plist value back to an NSDate:
NSString *Checkdate= [InfoDic objectForKey:#"LastDate"];
NSDateFormatter *DateFormat=[[NSDateFormatter alloc]init];
[DateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *theday=[DateFormat dateFromString:Checkdate];
This is the error log on my iPhone 4S:
<Error>: -[__NSDate length]: unrecognized selector sent to instance 0x1d8715e0
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSDate length]: unrecognized selector sent to instance 0x1d8715e0'
*** First throw call stack:
(0x31e5c2a3 0x39b7997f 0x31e5fe07 0x31e5e531 0x31db5f68 0x327c213d 0x327c208d 0x327c237b 0x23fe1 0x33c83595 0x33cc3d79 0x33cbfaed 0x33d011e9 0x2385d 0x33cc4ad9 0x33cc4663 0x33cbc84b 0x33c64c39 0x33c646cd 0x33c6411b 0x3597a5a3 0x3597a1d3 0x31e31173 0x31e31117 0x31e2ff99
0x31da2ebd 0x31da2d49 0x33cbb485 0x33cb8301 0x235a1 0x23528)
Please note that when I install the app on my device it takes the first date and stores it in the plist. When I close the app and rerun it, it gives me the SIGABRT.
What should I do about this?
You are not storing a date as a string in the plist, you are storing it as a date.
The line:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
stores the actual NSDate object.
All you need to get it back out is to call:
NSDate *theDay = InfoDic[#"LastDate"];
BTW - the line:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
should be:
[InfoDic setObject:[NSDate date] forKey:#"LastDate"];
or just:
InfoDic[#"LastDate"] = [NSDate date];
You are overcomplicating things. What makes you think that storing a NSDate object you'll get back a NSString?
Just do
NSDate * checkDate = [InfoDic objectForKey:#"LastDate"];
Also, don't confuse KVC methods with NSDictionary methods.
You want to use setObject:forKey: instead of setValue:forKey if you don't want to face bad surprises.

Can set to nil but not to a different thing

i think i don't finish to understand all about memory and that stuff but this is my problem:
I have a variable defined idActual on a view that will be pushed (var defined in its header), i can read (NSLog(idActual)) and set it to nil without problems. BUT when i change its value i get an CFString error, that its supposed to be due to bad memory management, i've tried this:
i can do this: nextView.idActual = nil;
i cant do this:
a) nextView.idActual = #"1";
b) NSString *str = [NSString stringWithFormat:#"1"];
nextView.idActual = str;
c) NSString *str = [[NSString alloc] initWithFormat:#"1"];
nextView.idActual = str;
[str release];
a, b and c always give me the CFString error:
*** -[CFString isEqualToString:]: message sent to deallocated instance
It appears that the CFString (NSString) that is contained in nextView.idActual has already been released when you go to change the value. If you can post more of the related code, that would help.
I'm guessing that idActual is declared as #property(nonatomic,retain). When you try to set a new value into idActual, the setter method for that property is called (It's possible that the setter was automatically generated). The first thing that the setter method is doing is trying to compare the old value and the new value - and then it crashes.
When that setter method attempts to compare the new value to the old value, it runs into trouble because the old value is already deallocated.
Are you calling [nextView.idActual release] before you assign these new values ? If you are, comment out that line, and see if that fixes your problem. The auto-generated setter method will release the old value for you.

&error error - iOS dev

I am trying to create an AVCaptureSession. I based my code on the WWDC 2011 video, number 419.
I have the following line which is exactly the same as the code in the WWDC 2011 video and it also identical to code here http://www.bardecode.com/en/knowledge-base/214-detailed-description-of-work-around-for-avcapturevideopreviewlayer-problem-in-ios-41.html
// Create a device input with the device and add it to the session.
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
But Xcode says that the &error is the use of an undeclared identifier.
This is because you've not defined the NSError error variable, that you're providing the address of when you use &error.
If you define the variable via...
NSError *error = nil;
...on the line before, all should be well.
As a bit of an explanation, if you look at the signature for the AVCaptureDeviceInput deviceInputWithDevice:error: method you'll see the following:
+ (id)deviceInputWithDevice:(AVCaptureDevice *)device error:(NSError **)outError
In other words, this method expects the address of an NSError pointer variable to be provided as the ourError parameter.

pathForResource doesn't work

I have problem with
NSString *filePaht = [[NSBundle mainBundle] pathForResource:(NSString *)name ofType:(NSString *)ext];
if I used
NSString *filePaht = [[NSBundle mainBundle] pathForResource:#"soundName" ofType:#"aiff"];
it's OK. but when I used
NSString *fileName = [[file.list objectAtIndex:index] objectForKey:#"soundName"];
NSString *filePaht = [[NSBundle mainBundle] pathForResource:fileName ofType:#"aiff"];
It's not work
have any idea !?
Thanks
I am going to guess that fileName from file.list includes the file extension. So you are searching for "soundName.aiff.aiff" which does not exist. Try passing #"" for type or stripping the extension from fileName:
fileName = [fileName stringByDeletingPathExtension];
Check your Debugger Console, as it may be telling what you're doing wrong.
[file.list objectAtIndex:index]
If you're getting an NSRangeException, it may be because index contains an index that is outside the bounds of the array. Remember that arrays in Cocoa are serial, not associative; if you remove an object, the indexes of all the objects that came after it will go down by 1, upholding the invariant that 0 ≤ (every valid index) < (count of objects in the array).
It could also be because you never declared a variable named index.
NSString *fileName = [[file.list objectAtIndex:index] objectForKey:#"soundName"];
NSString *filePaht = [[NSBundle mainBundle] pathForResource:fileName ofType:#"aiff"];
If nothing is happening or you get an NSInternalInconsistencyException, it could be one of:
fileList is nil.
The dictionary returned from [file.list objectAtIndex:index] does not have an object for the key soundName.
If you got a “does not respond to selector” message in the Console, it may be one of:
file.list is an object, but not an NSArray.
[file.list objectAtIndex:index] is not an NSDictionary.
fileName ([[file.list objectAtIndex:index] objectForKey:#"soundName"]) is not an NSString.
Remember that the class name you use when you declare the variable doesn't matter except to the compiler; at run time, it's just a variable holding a pointer to an object. The object can be of any class. It is perfectly valid to put something that isn't an NSString into an NSString * variable; it simply carries a very high (near certain) risk of wrong behavior and/or crashing shortly thereafter.
Such a crash will usually manifest in the form of a “does not respond to selector” exception (after something sends the object a message that NSString objects, for example, should respond to, but that the object doesn't respond to because it isn't an NSString).
Whichever problem you're having, you can use the Debugger to investigate.
Sorry with my fault.
I get data from XML file
and that data include "\n". yes I see "\n" so I replace with #""
but it not enough I must trim space value again.
Thanks for all advice ^_^

Resources