ios - Objective-c Json解析空数组错误还是Bug?
<p><p>当我使用 tableview 解析 json 时,如果有 json 项目,但如果没有加载 json 项目,并且当我单击返回按钮时,会出现此错误。</p>
<pre><code>erminating app due to uncaught exception 'NSRangeException', reason: '*** -: index 0 beyond bounds for empty array'
*** First throw call stack:
</code></pre>
<p>我认为当我单击快速后退按钮并在我的表格 View 代码下给出此错误时,json 不会加载。</p>
<pre><code> @interface MasterViewController ()
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalPages;
@property (nonatomic, assign) NSInteger totalItems;
@property (nonatomic, assign) NSInteger maxPages;
@property (nonatomic, strong) NSMutableArray *activePhotos;
@property (strong, nonatomic) NSMutableArray *staticDataSource;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
- (void)viewDidLoad
{
;
self.activePhotos = [ init];
self.searchResults = [ init];
self.staticDataSource = [ init];
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
returnself.activePhotos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row == ) {
cell = ;
UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *);
;
} else {
NSDictionary *photoItem = self.activePhotos;
cell = ;
cell.textLabel.text = ;
if (![ isEqual:]) {
cell.detailTextLabel.text = ;
}
}
return cell;
}
- (void)loadPhotos:(NSInteger)page
{
NSString *userismim =[ stringForKey:@"userisim"];
NSArray* words = ];
NSString* nospacestring = ;
NSLog(@"%@",nospacestring);
NSString *apiURL = ;
NSURLSession *session = ;
[
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
if (!error) {
NSError *jsonError = nil;
NSMutableDictionary *jsonObject = (NSMutableDictionary *);
NSLog(@"%@",jsonObject);
];
self.currentPage = [ integerValue];
self.totalPages= [ integerValue];
self.totalItems= [ integerValue];
self.activePhotos = self.staticDataSource;
dispatch_async(dispatch_get_main_queue(), ^{
;
});
}
}] resume];
}
</code></pre>
<p>感谢一切。我需要你的帮助。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您正在显示事件指示器,该指示器将一直旋转直到 <code>json</code> 加载。</p>
<p>如果您在 <code>json</code> 加载之前按下返回按钮,则会发生应用程序尝试为数组分配空引用,这是不可能的,因此会引发错误。</p>
<p>为避免这种情况,您可以在请求发出后停止 <code>userInteraction</code>,并仅在获得成功或失败响应后启用。</p>
<p>要禁用交互,请添加</p>
<pre><code>[ beginIgnoringInteractionEvents]
</code></pre>
<p>之后</p>
<pre><code>NSURLSession *session = ;
</code></pre>
<p>要再次启用,请添加:</p>
<pre><code> [ endIgnoringInteractionEvents]
</code></pre>
<p>之前</p>
<pre><code>if (!error) {
</code></pre>
<p>希望这能解决你的问题。</p></p>
<p style="font-size: 20px;">关于ios - Objective-c Json解析空数组错误还是Bug?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34019787/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34019787/
</a>
</p>
页:
[1]