Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to read an input file (if it exists) and then want to add a string to that input. My code looks as follows.

NSMutableArray *listData = [[NSMutableArray alloc] initWithContentsOfFile:*filepath*];
// listData = null if the input file does not exist.
NSString *jobName = [NSString stringWithFormat:@"My New Job"];
[listData addObject:jobName];

if the input exists then after addObject:jobName, the listData is updated but if the input file does not exist the listData still gives null after addObject:jobName. My input file (if exists) looks something like.

<array>
      <string>My Job 1</string>
      <string>My Job 2</string>
      <string>My Job 3</string>
</array>

I want to add the string in the existing array of strings or want to create a new array of string jobName if it is not already there. Can somebody help me out. Which method I should use to create a new array of string when the input file does not exist.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
720 views
Welcome To Ask or Share your Answers For Others

1 Answer

One of some possibilities:

if (!listData) listData = [[NSMutableArray alloc] init];
[listData addObject:jobName];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...