Objective-C creating a text file with a string

后端 未结 3 1186
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 01:05

I\'m trying to create a text file with the contents of a string to my desktop. I\'m not sure if I\'m doing it right, I don\'t get errors but it doesn\'t work either...

3条回答
  •  半阙折子戏
    2020-12-30 01:30

    You don't know if you're getting any errors because you're ignoring the returned YES/NO value of the -writeToFile:... method, and giving it no error pointer into which to record any possible failure. If the method returns NO, you'd check (and handle or present) the error to see what went wrong.

    At a guess, the failure is due to the path you constructed. Try -stringByAppendingPathComponent: instead of -stringByAppendingString: ... this and its related methods properly handle paths.

    The file probably is actually being created (ie, you might not be getting any errors after all). My guess is the file is created somewhere like "~/Desktopfile.txt" since your use of -stringByAppendingString: doesn't consider the string as slash-separated path. Check your home folder - I'll bet the file's there.

提交回复
热议问题