Program crashes at the line with comments below. Not sure what's wrong. Any tips for a 1st time programmer?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [[users objectAtIndex:indexPath.row] objectForKey:[[NSString alloc] initWithFormat:#"%d", indexPath.row]];
//My program runs but then crashes at this line, I have no idea what's wrong with it though "reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance"
return cell;
}
Some More Code (Not all of it though) As Requested
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
users = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[self.tableView reloadData];
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [users count];
}
In .h file
#interface GIFUsersListViewController : UITableViewController {
NSArray *users;
NSMutableData *data;
} #end
As of this message: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance it looks like users is not NSArray, its rather NSDictionary.
If you're not sure what is in JSON then check it before assigning to users:
id usersFromJSON = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
if ([usersFromJSON isKindOfClass:[NSArray class]]) {
users = usersFromJSON;
} else {
NSLog(#"Something wrong with my JSON: %#", usersFromJSON);
}
Related
This is my code, I want to display Json data on UITableview. My app Crash on due to This reason '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x10a860840. i am new to Json.
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error;
NSString *url_string = [NSString stringWithFormat: #"http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"];
NSURL *blogURL = [NSURL URLWithString:url_string];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSDictionary *dataDictionary = [NSJSONSerialization
JSONObjectWithData:jsonData options:0 error:&error];
//NSLog(#"Data : %#",dataDictionary);
for (NSDictionary *bpDictionary in dataDictionary)
{
NSobjectClass *currenHotel = [[NSobjectClass alloc]initWithcountrycode:[bpDictionary
objectForKey:#"countrycode"] name:[bpDictionary objectForKey:#"name"] toponmyName:[bpDictionary objectForKey:#"toponmyName"] population:[bpDictionary
objectForKey:#"population"]];
[self.objectHolderArray addObject:currenHotel];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
return [self.objectHolderArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Cell1 *cell = [self.JsonTableview dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
NSobjectClass *currentHotel = [self.objectHolderArray
objectAtIndex:indexPath.row];
cell.countryCode.text = currentHotel.countrycode;
cell.countryName.text = currentHotel.name;
cell.countrytype.text = currentHotel.toponmyName;
cell.population.text = currentHotel.population;
return cell;
}
Please check my Code.
I'm attempting to allow my user to be able to delete/remove a row from a tableView (remove an object from an existing NSMutableArray), however when I try and delete the row, I get the following crash error:
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: '-[__NSCFArray
removeObjectAtIndex:]: mutating method sent to immutable object'
Does anyone know why this might be? See code below. Apologies for the lengthy code.
.h
#property (strong, nonatomic) NSMutableArray *descripData;
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.descripData = [[NSMutableArray alloc] init];
refreshControl = [[UIRefreshControl alloc]init];
[self.tableView addSubview:refreshControl];
[refreshControl addTarget:self action:#selector(refreshTable) forControlEvents:UIControlEventValueChanged];
NSMutableDictionary *viewParams = [NSMutableDictionary new];
[viewParams setValue:#"storeditems" forKey:#"view_name"];
[DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.descripData = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#", [error localizedDescription]);
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self.storageData count] > 0 && self.descripData.count > 0)
{
return [self.descripData count];
}
else
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = #"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.storageData.count > 0 && self.descripData.count > 0) {
noitemsView.hidden = YES;
NSDictionary *title = [self.descripData objectAtIndex:indexPath.row];
[[cell itemName] setText:[title objectForKey:#"node_title"]];
NSDictionary *node = [self.descripData objectAtIndex:indexPath.row];
[[cell itemDescrip] setText:[node objectForKey:#"body"]];
NSDictionary *value = [self.descripData objectAtIndex:indexPath.row];
[[cell valueLabel] setText:[value objectForKey:#"dollarvalue"]];
NSLog(#"%#", self.descripData);
NSDictionary *quantity = [self.descripData objectAtIndex:indexPath.row];
[[cell quantityLabel] setText:[quantity objectForKey:#"quantity"]];
NSLog(#"%#", self.descripData);
NSString *secondLink = [[self.descripData objectAtIndex:indexPath.row] objectForKey:#"photo"];
[cell.itemPhoto sd_setImageWithURL:[NSURL URLWithString:secondLink]];
}
else {
noitemsView.hidden = NO;
}
return cell;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 152;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.descripData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ItemDetailViewController *detailViewController = [[ItemDetailViewController alloc] initWithNibName:#"ItemDetailViewController" bundle:nil];
detailViewController.itemDetail = [[values objectAtIndex:indexPath.row] objectForKey:#"node_title"];
detailViewController.itemDetail = [self.descripData objectAtIndex:indexPath.row];
detailViewController.secondLink = self.descripData[indexPath.row][#"photo"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
Check the line self.descripData = responseObject;. It looks like you are probably setting your descripData property to point to a NSArray, not an NSMutableArray. If you really need the mutability, try self.descripData = [responseObject mutableCopy]; instead.
I suspect that the following assignment causes the problem:
self.descripData = responseObject;
The response object itself is immutable, assigning it to a NSMutableArray does not change that fact, you need to create a NSMutableArray with the contents of the responseObject:
self.descripData = [NSMutableArray arrayWithArray:responseObject];
Most likely happening on the following line:
self.descripData = responseObject;
You're replacing your previous mutable array for descripData with a new one, which apparently is not mutable. You can either create a new mutable array from this new array using [NSMutableArray arrayWithArray:responseObject], or you can add the contents of responseObject to your current mutable array.
It's important to pay attention when you assign something of type id to a variable, because there's no compile-time type checking that can occur. This sometimes results in errors at runtime.
Face this issue because NSUserDefaults always returns an immutable object.
Please while retrieving Mutable object from NSUserDefaults please refer below code.
NSMutableArray *storeArray = [[defaultDefects objectForKey:#"defaultStoreDefects"]mutableCopy];
I'm very new to Obj-c and I'm now creating a tableview to show what I have parsed from Json. Here's the code:
#import "ClasstableViewController.h"
#interface ClasstableViewController ()
#end
#implementation ClasstableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"JSONRead";
NSString *testUrl = #"example.com";
NSURL *url = [NSURL URLWithString:testUrl];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10];
NSData *JSONData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSArray *jsonResult = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];
self.data = jsonResult;
NSMutableArray *_names = [NSMutableArray array];
for (id item in jsonResult)
[_names addObject:[NSString stringWithFormat:#"%#", item[#"Mon"]]];
self.names = _names;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = self.names[indexPath.row];
return cell;
}
and I have change my tableview to the correct class in the identity inspector to ClasstableViewController, I see others have resolve their issues with this solution.
but I'm still getting this:
-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fd8835691c0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fd8835691c0'
anyone have any idea why?
Probably your NSArray *jsonResult is an array of NSStrings, or contain some NSStrings as objects. So when you do
for (id item in jsonResult)
[_names addObject:[NSString stringWithFormat:#"%#", item[#"Mon"]]];
you are supposing that item is a NSDictionary, but it isn't.
for (id item in jsonResult)
[_names addObject:[NSString stringWithFormat:#"%#", item[#"Mon"]]];
item is NSString not NSDictionary,use like below
for (id item in jsonResult)
[_names addObject:[NSString stringWithFormat:#"%#", item]];
Use JSONEditor to check the JSON.
I am recieving this error when i scroll to the bottom of my TableView, I dont think its any error with actually retrieving the pictures from the server.:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (15) beyond bounds (15)'
Here is my .m file I cut it to only the actually needed parts of the file:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self entries] count] + tweets.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *created = [tweet objectForKey:#"created_at"];
NSLog(#"%#", created);
static NSString *CellIdentifier = #"TweetCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *text = [tweet objectForKey:#"text"];
NSString *name = [[tweet objectForKey:#"user"] objectForKey:#"name"];
cell.textLabel.text = text;
cell.detailTextLabel.text = [NSString stringWithFormat:#"by %#", name];
return cell;
}else {
static NSString *CellIdentifier = #"InstagramCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *entry = [self entries][indexPath.row];
NSString *imageUrlString = entry[#"images"][#"low_resolution"][#"url"];
NSURL *url = [NSURL URLWithString:imageUrlString];
[cell.imageView setImageWithURL:url];
return cell;
}
}
- (void)fetchTweets {
self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:#"https://api.twitter.com/1.1/"] key:#"TWEETER_KEY" secret:#"TWEETER_SECRET"];
[self.twitterClient authorizeUsingOAuthWithRequestTokenPath:#"/oauth/request_token" userAuthorizationPath:#"/oauth/authorize" callbackURL:[NSURL URLWithString:#"floadt://success"] accessTokenPath:#"/oauth/access_token" accessMethod:#"POST" scope:nil success:^(AFOAuth1Token *accessToken, id responseObject) {
[self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self.twitterClient getPath:#"statuses/home_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *responseArray = (NSArray *)responseObject;
[responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(#"Success: %#", obj);
tweets = responseArray;
[self.tableView reloadData];
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
} failure:^(NSError *error) {
NSLog(#"Error: %#", error);
}];
}
There needs to be tight coordination between the return value from numberOfRowsInSection and the array access that the code does in cellForRowAtIndexPath.
Consider this, your entries array and tweets array each have 4 elements. So numberOfRowsInSection returns 8. The cellForRowAtIndexPath method gets called to configure row 6. Your code will do this: NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
But wait... that array has only 4 elements, right? Asking for something at index 6 will generate the crash you see.
It might be simpler to write a methods to interleave the arrays into a single array, then answer the count of the combined array in numberOfRowsInSection. In cellForRowAtIndexPath, the array elements themselves should be able to tell you what kind of row you have (not the index). Dereference the combined array and configure the table accordingly.
EDIT - I'll try to make my advice more explicit in code: Let's say, for simplicity, that "entries" and "tweets" are both arrays of NSDictionaries and that your app wants to organize them in the UI entries first, then tweets.
// in interface:
#property (nonatomic, strong) NSArray *myModel;
// in code:
- (NSArray *)myModel {
if (!_myModel) {
NSMutableArray *array = [NSMutableArray arrayWithArray:[self entries]];
[array addObjectsFromArray:tweets];
_myModel = [NSArray arrayWithArray:array];
}
return _myModel;
}
We call this 'myModel' for a reason. It's the datasource of the table. The datasource protocol is asking explicitly about this array (and no other).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.myModel.count;
}
Now cellForRowAtIndexPath is going to ask you to configure that many (myModel count) rows, numbered 0..count-1. You must dereference the same array -- myModel -- for all datasource methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *myModelForThisRow = self.myModel[indexPath.row];
// get the cell = deque...
cell.textLabel.text = myModelForThisRow[#"someKey"];
return cell;
}
What if your tweets or entries array changes? No problem, just rebuild the model like this:
- (IBAction)tweetsOrEntriesDidChange:(id)sender {
self.myModel = nil; // the "lazy" getter will rebuild it
[self.tableView reloadData]; // this will call the datasource which will call the lazy getter
}
You are trying to go read into an array outside of it's bounds.
That array access look very suspicious
if (indexPath.row % 2 == 0) {
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
as well as this one
NSDictionary *entry = [self entries][indexPath.row];
From what I've seen your array tweets and [self entries] don't contain as many object each as there is row in your table section.
I take my assomption from here :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self entries] count] + tweets.count;
}
NSRangeException is thrown because you are trying to access an index which is not within the valid range for your array. Try setting an "Exception breakpoint" in Xcode to see where it's coming from. Check here to know more about Exception breakpoints
This is typically caused by an off by one error.
I am new to iphone development, I am trying to load a NSMutableArray values into a table view, I am using code below for this which generates error as specified. Can some one help me in rectifying this error.
code :-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [ShowList count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSString *cellValue = [ShowList objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
error :-
2011-02-25 07:22:24.470 iPhone[1032:207] -[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0xab15d30
2011-02-25 07:22:24.471 iPhone[1032:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0xab15d30'
*** Call stack at first throw:
Print Description of My NSMutableArray :-
2011-02-25 07:21:54.806[1032:207] (
(
"viral_tweeter",
default1571546,
default1570056,
twilightsaga,
"wp-monetizer",
viraltweetbuild,
"building_a_list",
yourtwittertips,
"twitter_profit",
mikesbi,
mikesbizz,
default1164341,
incbizztest,
default1164319,
iprotv,
iwantafreecopy1,
tweeterbuilder,
trafficlists,
myadsensenews,
mysafelistnews,
myviralnews,
safelistology,
slmembers,
slpmembers,
twonderlandlist,
noseospider,
yseospider,
digitallockdown,
alistblueprint,
classifiedtips,
incbizzblog,
"xit-trafficbeta",
twwidget,
jvtrafficfunnel,
instantmlmspage,
listbuldingmax,
"incbizz_tips"
)
)
code for parsing HTTP Get response :-
- (void)requestDataFetcher:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *)error {
// this is only for testing whether the data is coming or not
// NSDictionary *tempDict = [GTMOAuthAuthentication dictionaryWithResponseData:data];
if (error)
{
NSLog(#"Error: in getting data after authentication : %#",[error description]);
}
else
{
// NSLog(#"Succcess: in getting data after authentication \n data: %#",[tempDict description]);
NSString* aStr;
aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSDictionary *dictionary = [aStr JSONValue];
NSArray *keys = [dictionary allKeys];
Names = [[NSMutableArray alloc]init];
int i = 0;
// values in foreach loop
for (NSString *key in keys)
{
i++;
NSArray *items = (NSArray *) [dictionary objectForKey:key];
// NSLog(#" test %#", items);
if (i==3)
{
for (NSString *item in items)
{
NSString* aStrs= item;
// NSLog(#" test %#", aStrs);
NSDictionary *dict = aStrs;
NSArray *k = [dict allKeys];
for (id *it in k)
{
// NSLog(#"the child item: %#", [NSString stringWithFormat:#"Child Item -> %# value %#", (NSDictionary *) it,[dict objectForKey:it]]);
NSString *value = [it description];
if ( [value isEqualToString:#"name"])
{
NSString * value = (NSString*)[[dict objectForKey:it] description];
NSLog(value);
[Names addObject:value];
[[MySingletonClass sharedMySingleton] SetAweberList: value];
}
}
}
}
}
mShowList.hidden = FALSE;
}
}
#Ravi your array is array of array that is the ShowList is an array which has first object as array and that array is that
"viral_tweeter",
default1571546,
default1570056,
twilightsaga,
"wp-monetizer",
viraltweetbuild,
"building_a_list",
yourtwittertips,
"twitter_profit",
mikesbi,
mikesbizz,
default1164341,
incbizztest,
default1164319,
iprotv,
iwantafreecopy1,
tweeterbuilder,
trafficlists,
myadsensenews,
mysafelistnews,
myviralnews,
safelistology,
slmembers,
slpmembers,
twonderlandlist,
noseospider,
yseospider,
digitallockdown,
alistblueprint,
classifiedtips,
incbizzblog,
"xit-trafficbeta",
twwidget,
jvtrafficfunnel,
instantmlmspage,
listbuldingmax,
"incbizz_tips"
so you can do something like this.
NSString *cellValue = [[ShowList objectAtIndex:0] objectAtIndex:indexPath.row];
I assume UILabel's setText calls isEqualToString for some reason, so it crashes for the very first object in your array which is not a NSString.