Intereting Posts

Viewcontroller.m не выполняет код

Я только что сделал проект, и я использовал код с этого сайта:

И проблема, конечно же, в ViewController.m. Код в нем не выполняется, я попытался поставить NSLog(@"Test With NSLog"); в ViewController.m, но ничего не происходит. Вещь JSON с сайта «site_url» хороша.

Вот весь код:


 #import <UIKit/UIKit.h> #import "HomeModel.h" @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, HomeModelProtocol> @property (weak, nonatomic) IBOutlet UITableView *listTableView; @end 


 #import "ViewController.h" #import "Location.h" @interface ViewController () { HomeModel *_homeModel; NSArray *_feedItems; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Set this view controller object as the delegate and data source for the table view self.listTableView.delegate = self; self.listTableView.dataSource = self; // Create array object and assign it to _feedItems variable _feedItems = [[NSArray alloc] init]; // Create new HomeModel object and assign it to _homeModel variable _homeModel = [[HomeModel alloc] init]; // Set this view controller object as the delegate for the home model object _homeModel.delegate = self; // Call the download items method of the home model object [_homeModel downloadItems]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)itemsDownloaded:(NSArray *)items { // This delegate method will get called when the items are finished downloading // Set the downloaded items to the array _feedItems = items; // Reload the table view [self.listTableView reloadData]; } #pragma mark Table View Delegate Methods - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of feed items (initially 0) return _feedItems.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Retrieve cell NSString *cellIdentifier = @"BasicCell"; UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; // Get the location to be shown Location *item = _feedItems[indexPath.row]; // Get references to labels of cell myCell.textLabel.text = item.username; return myCell; } @end 


 #import <Foundation/Foundation.h> @protocol HomeModelProtocol <NSObject> - (void)itemsDownloaded:(NSArray *)items; @end @interface HomeModel : NSObject <NSURLConnectionDataDelegate> @property (nonatomic, weak) id<HomeModelProtocol> delegate; - (void)downloadItems; @end 


 #import "HomeModel.h" #import "Location.h" @interface HomeModel() { NSMutableData *_downloadedData; } @end @implementation HomeModel - (void)downloadItems { // Download the json file NSURL *jsonFileUrl = [NSURL URLWithString:@"site_url"]; // Create the request NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl]; // Create the NSURLConnection [NSURLConnection connectionWithRequest:urlRequest delegate:self]; } #pragma mark NSURLConnectionDataProtocol Methods - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // Initialize the data object _downloadedData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Append the newly downloaded data [_downloadedData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // Create an array to store the locations NSMutableArray *_locations = [[NSMutableArray alloc] init]; // Parse the JSON that came in NSError *error; NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error]; // Loop through Json objects, create question objects and add them to our questions array for (int i = 0; i < jsonArray.count; i++) { NSDictionary *jsonElement = jsonArray[i]; // Create a new location object and set its props to JsonElement properties Location *newLocation = [[Location alloc] init]; newLocation.username = jsonElement[@"Username"]; = jsonElement[@"Email"]; = jsonElement[@"money"]; // Add this question to the locations array [_locations addObject:newLocation]; } // Ready to notify delegate that data is ready and pass back items if (self.delegate) { [self.delegate itemsDownloaded:_locations]; } } @end 


 #import <Foundation/Foundation.h> @interface Location : NSObject @property (nonatomic, strong) NSString *username; @property (nonatomic, strong) NSString *email; @property (nonatomic, strong) NSString *money; @end 


 #import "Location.h" @implementation Location @end