该应用程序在游戏中保持得分。根据您的分数,它将使用GET方法从在线数据库中检索报价并以JSON格式返回。例如,你的得分是5,得到1分,10,得到2,依此类推。显示引用的视图是UIViewController
,其中包含UITextView
。
我有一个基于分数运行的for loop
,在1.5秒延迟后一遍又一遍地运行相同的GET请求,因此容纳数据库的服务器不会拒绝几乎同时发出的请求。
我创建了一些NSStrings并从JSON数据中提取信息,将其附加到一些基本HTML代码中,然后将其设置为UITextView attributedText
。
大多数情况下这种情况很好,但每隔一段时间,我就会期待2个引号,而且只会得到1,或者某些引号最终会相同。
有人可以告诉我,如果有更好的方法可以做到这一点,而不是我现在的状态吗?
- (void)viewWillAppear:(BOOL)animated {
if ([textView.text isEqualToString:@""]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger getReady = [defaults integerForKey:@"after"];
self.theNumber = getReady;
for(int i = 0; i< self.theNumber; i++) {
[self performSelector:@selector(quoteView) withObject:self afterDelay:1.5 ];
}
}
}
-(void) quoteView {
NSString *bringitalltogether = @"http://url.com&type=json";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:bringitalltogether]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
[request setHTTPMethod:@"GET"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if ([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
if (code == 200){
}
else
{
UIAlertView *oops = [[UIAlertView alloc] initWithTitle:@"Oops" message:@"The network is having difficulties getting you the quote. Please check your network settings and try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[oops show];
}
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSMutableDictionary *allResults = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:nil];
NSArray *book = [allResults valueForKey:@"bookname"];
self.bookstring = [book objectAtIndex:0];
NSArray *chapter = [allResults valueForKey:@"chapter"];
self.chapterstring = [chapter objectAtIndex:0];
NSArray *verse = [allResults valueForKey:@"verse"];
self.versestring = [verse objectAtIndex:0];
NSArray *text = [allResults valueForKey:@"text"];
self.textstring = [text objectAtIndex:0];
[self doneGotIt];
}
- (void) doneGotIt {
if (!self.theArray) {
self.theArray = [[NSMutableArray alloc] init];
}
NSString *doIt = [NSString stringWithFormat:@"%@ - %@ %@:%@", self.textstring, self.bookstring, self.chapterstring, self.versestring];
[self.theArray addObject:doIt];
NSString *theEnd = [self.theArray componentsJoinedByString:@"\n"];
NSString *loadHTML = [@"<head> <style type='text/css'>a > img {pointer-events: none;cursor: default;}</style></head><b><div align=\"left\"><font size=5>" stringByAppendingString:theEnd];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[loadHTML dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
textView.attributedText = attributedString;
NSLog(@"ARRAY: %@", self.theArray);
NSLog(@"String: %@", theEnd);
}
-(IBAction)finished {
[self dismissViewControllerAnimated:YES completion:nil];
textView = nil;
}
从NSLogs
到最后,有时NSMutableArray
包含几个相同的引号,这就是为什么它们不会在字符串中显示,因为它可以消除重复。我的问题是,是否有更好的方法可以防止这些错误发生?
答案 0 :(得分:0)
这是一些伪代码
mutableArray = new NSMutableArray
while([mutableArray count] < total) {
quote = getQuote()
if([array indexOfObject:quote] != NSNotFound)
[mutableArray addObject:quote]
}
这将确保您没有重复的引号。获得有效引号数组后,可以按照自己的方式构造字符串。