每次触摸屏幕都会更新 plist 中的文本
我正在从 plist 中搜索一些短语,并希望每次触摸时都将此类语句更新到屏幕上,但我找不到问题的解决方案,因此我请求帮助以了解如何实现此目的,我的代码如下。
int RecordIndexDitados = 0;
NSMutableArray *dictDitados;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Ditados.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
NSDictionary *ditados = [plistData objectForKey:@"Ditados"];
NSMutableArray *selection = [[ditados objectForKey:@"Selection"] mutableCopy];
RecordIndexDitados = arc4random()%[selection count];
// Select and display currently selected record from the array.
dictDitados = [selection objectAtIndex:RecordIndexDitados]
CCLabel *ditados = [CCLabel labelWithString:@"Question"
dimensions:CGSizeMake(400, 200)
alignment:UITextAlignmentCenter
fontName:@"Brush Script" fontSize:36];
ditados.color = ccc3(0, 74, 128);
[ditados setPosition:ccp(240, 120)];
[self addChild: ditados];
[ditados setString:[NSString stringWithFormat:@"%@",dictDitados]];
<plist version="1.0">
<dict>
<key>Ditados</key>
<dict>
<key>Selection</key>
<array>
<string>Text 1</string>
<string>Text 2</string>
<string>Text 3</string>
</array>
</dict>
</dict>
</plist>
I'm searching for some phrases from a plist, and would like to update such statements to the screen every touch, but I can not find a solution to the problem, so I ask help on how to implement this, my code is below.
int RecordIndexDitados = 0;
NSMutableArray *dictDitados;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Ditados.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
NSDictionary *ditados = [plistData objectForKey:@"Ditados"];
NSMutableArray *selection = [[ditados objectForKey:@"Selection"] mutableCopy];
RecordIndexDitados = arc4random()%[selection count];
// Select and display currently selected record from the array.
dictDitados = [selection objectAtIndex:RecordIndexDitados]
CCLabel *ditados = [CCLabel labelWithString:@"Question"
dimensions:CGSizeMake(400, 200)
alignment:UITextAlignmentCenter
fontName:@"Brush Script" fontSize:36];
ditados.color = ccc3(0, 74, 128);
[ditados setPosition:ccp(240, 120)];
[self addChild: ditados];
[ditados setString:[NSString stringWithFormat:@"%@",dictDitados]];
<plist version="1.0">
<dict>
<key>Ditados</key>
<dict>
<key>Selection</key>
<array>
<string>Text 1</string>
<string>Text 2</string>
<string>Text 3</string>
</array>
</dict>
</dict>
</plist>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不将 plist 加载到内存中并从那里更新内容呢?这样您只需从 plist 加载一次数据..而不是每次触摸屏幕时。
why don't you load your plist in memory and update your content from there? this way you load your data from the plist only once..not every time you touch the screen.
1st 将 plist 的内容读入您设计的 plist 中的一些临时对象(字符串、数组或目录)。
第二知道将更新的值插入到临时对象中。
第三将临时对象分配给主对象。
第四步将主对象写入文件。
为了您更好地理解,请参阅:
http://iphonesdevsdk.blogspot.com/2011/04 /plist.html
它可能对你有帮助。
1st read the contents of plist into some temp object(string,array or directory) as you designed plist.
2nd know insert the updated value into temp object.
3rd assign the temp object to main object.
4th write the main object into file.
For your better understanding refer this like:
http://iphonesdevsdk.blogspot.com/2011/04/plist.html
it may helps you.