1

Here i have demo code for saving NSMutableString in to File (FileName.dat)

NSError* error = nil;
NSMutableString* dat = [[NSMutableString alloc] initWithCapacity:1];
BOOL result = [dat writeToFile:@"FileName.dat" atomically:YES encoding:NSUTF8StringEncoding error:&error];

but i have two different output while rung it in iOS7 and iOS8beta5

Output XCode5+iOS7 enter image description here

Output XCode5+iOS8 enter image description here


While running it in iOS7 it shows that there in an error in parsing file path, but in iOS8beta5 it crash by saying that [NSFileManager fileSystemRepresentationWithPath:] have nil or empty path.
Question :
In both SDK iOS8 and iOS7 it take NSError as argument to return error, so i believe that it should return error instead of crashing application
Is apple mansion any changes regarding it, If yes then please give me reference link for the same.
Jageen
  • 6,345
  • 2
  • 37
  • 56
  • the exception is correct: where is your path? the `Filename.dat` is not a valid path, but a random filename only. – holex Aug 18 '14 at 08:30
  • @holex i am not asking why exception occur i am asking why application crash,why different behavior, is any one have link in which apple have mansion about above the changes – Jageen Aug 18 '14 at 08:49
  • Same behaviour on NSMutableDictionary: a call that worked fine on iOs7 is now crashing with the same error you are reporting in iOs8 (and I'm not running a beta...) – Rick77 Sep 22 '14 at 12:30
  • you are right i also have same problem in iOS8, i just put code in try catch, apple did not mansion any changes in API. – Jageen Sep 22 '14 at 12:32
  • 1
    whether using a relative path was supported before iOs7 (and Apple took it away) or we have just been lucky until now, I guess all we can do is suck it, up and get to work to fix all our code :) – Rick77 Sep 22 '14 at 12:40

2 Answers2

2

The path you pass to [NSData writeToFile:atomically:] is not complete and should be a full path.

That is normally done by getting the path to the Documents folder and appending that filename.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • I know, but my question is it's not crashing my application in iOS7, but the same it not happen in iOS8beta5, – Jageen Aug 18 '14 at 08:31
  • 1
    @Jageen I guess that's simply *undefined behaviour*. The point is the code is wrong, regardless of how a particular version of iOS reacts. – trojanfoe Aug 18 '14 at 08:33
  • in both SDK iOS8 and iOS7 it take NSError as argument to return error, so i believe that it should return error instead of crashing application, – Jageen Aug 18 '14 at 08:35
  • @Jageen Well what is it you are trying to achieve? If it's a working app then use the correct code. If it's confirmation of a bug in iOS 8 then file a bug report with Apple. – trojanfoe Aug 18 '14 at 08:37
  • I can not accept answer, as it now sholve my problem What i can do is upvote, nothing else, Sorry – Jageen Aug 18 '14 at 08:47
2

The problem is your filename, @"FileName.dat", there is no path to the directory to save to. NSFileManager does not do this itself, you'll want to save to the Documents folder normally. Here is the code I usually use:

[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:myFileName];
MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • I know, but my question is it's not crashing my application in iOS7, but the same it not happen in iOS8beta5 – Jageen Aug 18 '14 at 08:32
  • @Jageen Doesn't matter, you claim that on iOS 7 instead of crashing it outputs an error message. Same difference. iOS 8 is in beta, please bug report this if you think that it should not throw an exception, rather just log an error message. Either way, it's not like your data is getting saved, I'm just showing you how to achieve that. – MCKapur Aug 18 '14 at 08:34
  • @Jageen Use this code, tell me if it works. If it does, accept! I stopped your crash :) – MCKapur Aug 18 '14 at 08:34
  • I can not accept answer, as it now sholve my problem What i can do is upvote, nothing else, Sorry – Jageen Aug 18 '14 at 08:46
  • @Jageen I'm having trouble understanding **why**... if the problem is that this **incorrect** code is crashing in iOS 8, but on iOS 7 it just throws an error message, that is **unexpected beta behaviour**, because you know - **it's a beta!**. So **file a bug** to Apple. **However**, disregarding whatever **error handling method Apple uses**, your code **won't actually work**. Like it won't save the file. You want to save the file right!? This code should **work**, it should **do what you asked for** (saving a file)! – MCKapur Aug 18 '14 at 09:36
  • I have application submitted to client,changing code lead me to test all related test cases, i don't want to change code, if i found that it's apple bug and it will solve in next release, if it's new SDK change then i want reference link. – Jageen Aug 18 '14 at 10:06
  • MCKapur: the code by Jageen (and mine for what it's worth) is bugged, point taken, the problem is that the new version of iOs 8 (and we are not talking about a beta anymore...) changed a behaviour that was supported in earlier version. To know who to blame (us, for relying on an undocumented feature, or Apple for taking away a documented feature among the folds of a new release) we should backtrack the api documentation and see how the methods where like, e.g in iOs5 (good look with that...). +1 for the answer, anyway (it should be accepted). – Rick77 Sep 22 '14 at 12:37
  • Found what misled me (and probably other) into using relative path names in writeToFile and such: Discussion *All relative pathnames refer implicitly to the current working directory. Changing the current working directory affects only paths created in the current process.* (from the NSFileManager changeCurrentDirectory: documentation). Quite a definitive statement (still misleading). – Rick77 Sep 22 '14 at 16:47