get crashed when deference subclassed NSObject - ios

#interface PADiscover : NSObject
#property (nonatomic, assign) unsigned int dw3C;
#property (nonatomic, assign) BOOL isSet;
#property (nonatomic, assign) PAContactModel model;
#end
In another VC
#property (nonatomic, strong) NSMutableDictionary *shakeDict;
Then I add some objects to shakeDict
- (void)viewDidLoad {
NSNumber *num = [NSNumber numberWithUnsignedInt:message.sDeviceInfo.dw3CId];
PADiscover *discover = [[PADiscover alloc] init];
discover.dw3C = message.sDeviceInfo.dw3CId;
discover.model = message.sDeviceInfo.dwDeviceType;
discover.isSet = message.sDeviceInfo.fgPasswdFlag;
[_shakeDict setObject:discover forKey:[NSString stringWithFormat:#"%#", num]];
[_shakeTV reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
PADiscover *discover = [[_shakeDict allKeys] objectAtIndex:indexPath.row];
**cell.textLabel.text = [NSString stringWithFormat:#"ID %u, ISSet %d", discover.dw3C, discover.isSet];//crashed!**
cell.detailTextLabel.text = [NSString stringWithFormat:#"model %d", discover.model];
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:11];
return cell;
}
It seems cannot deference PADiscover successfully,but I'm not sure when place I get some wrong.
can anyone explain that for me?

Try this,
NSString *key = [[_shakeDict allKeys] objectAtIndex:indexPath.row];
PADiscover *discover = [_shakeDict objectForKey:key];
instead of
PADiscover *discover = (PADiscover *)[[_shakeDict allKeys] objectAtIndex:indexPath.row];

At least this line may be error:
#property (nonatomic, assign) PAContactModel model;
change to strong if a model class.
#property (nonatomic, strong) PAContactModel model;

Related

Exception while typing into search bar on iOS

I'm taking over some code since another dev has moved on and his search bar has an error. I've searched the other threads for help on this, but none of them seem to be my error. Here is a screen shot of mine and the code.
http://imgur.com/gO6wuPb
2014-09-17 11:25:34.665 Moca[11388:60b] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key itemName.'
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [[[BNRItemStore sharedStore] allItems] count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 71;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"UITableViewCell";
UITableViewCell *cell = (UITableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//there are two tableviews involved here - one is the main one, and one is associated with searchDisplayController
//self.tableView is set automatically between the main one and the search one.
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
item = [searchResults objectAtIndex:indexPath.row];
} else {
item = items[indexPath.row];
}
if ([item.subcategory isEqual:#"Urgent"]) {
cell.imageView.image=[UIImage imageNamed:#"Urgent.png"];
}
else if ([item.subcategory isEqual:#"Emergency"]) {
cell.imageView.image=[UIImage imageNamed:#"Emergency.png"];
}
else if ([item.subcategory isEqual:#"Standard"]) {
cell.imageView.image=[UIImage imageNamed:#"Standard.png"];
}
else if ([item.subcategory isEqual:#"Normal"]) {
cell.imageView.image=[UIImage imageNamed:#"Normal.png"];
}
else {
cell.imageView.image=[UIImage imageNamed:#"Informational.png"];
}
cell.textLabel.text = item.changeOrder;
cell.detailTextLabel.text =item.title;
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSArray *items = [[BNRItemStore sharedStore] allItems];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"itemName contains[c] %#", searchText];
searchResults = [items filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
EDIT add item details
#import <Foundation/Foundation.h>
#interface BDDetailedChange : NSObject
+ (instancetype)randomItem;
- (instancetype)initWithItemName:(NSString *)name
valueInDollars:(NSString *)value
serialNumber:(NSString *)sNumber;
#property (nonatomic, copy) NSString *itemName;
#property (nonatomic, copy) NSString *serialNumber;
#property (nonatomic, copy) NSString *valueInDollars;
#property (nonatomic, readonly, strong) NSDate *dateCreated;
#property (nonatomic, copy) NSString *subcategory; //Emergency
#property (nonatomic, copy) NSString *service;
#property (nonatomic, copy) NSString *associatedCIs;
#property (nonatomic, copy) NSString *environment;// Production
#property (nonatomic, copy) NSString *approvalGroup;
#property (nonatomic, copy) NSString *initiator;
#property (nonatomic, copy) NSString *coordinator;
#property (nonatomic, copy) NSString *riskLevel;// Low
#property (nonatomic, copy) NSString *performingGroup;
#property (nonatomic, copy) NSString *implementationPlan;
#property (nonatomic, copy) NSString *validationPlan;
#property (nonatomic, copy) NSString *recoveryScope;
#end
BNR item .h
#interface BNRItem : NSObject
{
NSString *_approverEid;
NSString *_assignmentGroup;
NSString *_changeOrder;
NSString *_subcategory; //Emergency
NSString *_title; //title
}
- (void)setApproverEid: ( NSString *)aEid;
- (NSString*)approverEid;
- (void)setAssignmentGroup: ( NSString *)aGrp;
- (NSString*)assignmentGroup;
- (void)setChangeOrder: ( NSString *)co;
- (NSString*)changeOrder;
- (void)setSubcategory: ( NSString *)sub;
- (NSString*)subcategory;
- (void)setTitle: ( NSString *)title;
- (NSString*)title;
#end
BNRItem.m
#import "BNRItem.h"
#implementation BNRItem
- (void)setApproverEid:(NSString *)aEid
{
_approverEid = aEid;
}
- (void)setAssignmentGroup: ( NSString *)aGrp
{
_assignmentGroup = aGrp;
}
- (void)setChangeOrder: ( NSString *)co
{
_changeOrder=co;
}
- (void)setSubcategory: ( NSString *)sub
{
_subcategory= sub;
}
- (void)setTitle: ( NSString *)title
{
_title = title;
}
- (NSString*)approverEid
{
return _approverEid;
}
- (NSString*)assignmentGroup
{
return _assignmentGroup;
}
- (NSString*)changeOrder
{
return _changeOrder;
}
- (NSString*)subcategory
{
return _subcategory;
}
- (NSString*)title
{
return _title;
}
#end

myClass copyWithZone unrecognized selector sent to instance

When trying to get a object from an mutable array to et the values for that row in a tableview I get the error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[KCNote copyWithZone:]:
unrecognized selector sent to instance 0x15589f60'
Below is my code, I had a look about this error and most places talk about issues copying data structures from the object to the new one however the objects contents is just intergers and strings as you can see below.
KCNote.h
#import <Foundation/Foundation.h>
#interface KCNote : NSObject
#property (nonatomic) NSInteger NoteID;
#property (nonatomic, strong) NSString *Note;
#property (nonatomic, strong) NSString *Author;
#property (nonatomic, strong) NSString *Date;
#end
Code in my view controller for the table.
NSMutableArray *Notes;
- (void)viewDidLoad
{
Notes = [[NSMutableArray alloc] init];
...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"notecell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
// Configure the cell...
KCNote *note = [Notes objectAtIndex:indexPath.row];
UILabel *Author =(UILabel *) [cell viewWithTag:1];
UILabel *Date = (UILabel *) [cell viewWithTag:2];
UILabel *Note = (UILabel *) [cell viewWithTag:3];
Author.text = note.Author;
Date.text = note.Date;
Note.text = note.Note;
return cell;
}
Notes are added to the array using:
-(void)getNotes:(NSInteger)courseid{
[Notes removeAllObjects];
FMResultSet *s;
s = [db executeQuery:#"SELECT * FROM notes WHERE CourseID =?;", [NSString stringWithFormat:#"%d", courseid]];
//s = [db executeQuery:#"SELECT * FROM notes;"];
if ([db hadError]) {
NSLog(#"DB Error %d: %#", [db lastErrorCode], [db lastErrorMessage]);
}
while ([s next]) {
NSInteger NoteID = [s intForColumnIndex:0];
NSString *Note = [s stringForColumnIndex:2];
NSString *Author = [s stringForColumnIndex:3];
NSString *date = [s stringForColumnIndex:4];
KCNote *note = [KCNote new];
note.NoteID = NoteID;
note.Note = note;
note.Author = Author;
note.Date = date;
[Notes addObject:note];
}
[self.tbl_notes reloadData];
}

UITableView got null items when scrolling

I have a small application, where i'm trying to load some data in UITableView from a sqlite database. Apparently everything works fine, but when I try to scroll i get Null data.
My code:
self.tblContacts.dataSource = self;
self.tblContacts.delegate = self;
self->mainManager = [[MainManager alloc] init: [NSString stringWithFormat:#"%#/contacts.db", resourcePath]];
self->contactList = [[ContactManager alloc] init];
self->contactList = [mainManager loadContacts];
-----------------------------------------------------
- (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->contactList getContactsCount];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellID = #"Cell";
CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!Cell)
Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
Cell.lblCompany.text = [[self->contactList getContact:indexPath.row] company];
Cell.lblContact.text = [NSString stringWithFormat:#"%# %#", [[self->contactList getContact:indexPath.row] name], [[self->contactList getContact:indexPath.row] surname]];
return Cell;
}
ScreenShot:
If anyone can enlighten me please. Thanks in advance.
Best regards.
I solved it. I'm noob in objective-c and i need to learn more about (strong, weak, retain, assign). I just replace weak by retain in my Contact Class:
#property (retain) NSString *name;
#property (retain) NSString *surname;
#property (retain) NSString *company;
#property (retain) NSString *cif;
#property (retain) NSString *email;
#property (retain) NSString *address;
#property (retain) NSString *city;
#property (retain) NSString *telephone;
#property (retain) NSString *provider;
strong = retain:
it says "keep this in the heap until I don't point to it anymore" in
other words " I'am the owner, you cannot dealloc this before aim fine
with that same as retain"
weak:
it says "keep this as long as someone else points to it strongly"
Source: Objective-C ARC: strong vs retain and weak vs assign
Now works perfectly!
Thanks anyway, regards.
Try this,
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellID = #"Cell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *) currentObject;
break;
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Cell.lblCompany.text = [[self->contactList getContact:indexPath.row] company];
Cell.lblContact.text = [NSString stringWithFormat:#"%# %#", [[self->contactList getContact:indexPath.row] name], [[self->contactList getContact:indexPath.row] surname]];
return Cell;
}

Memory keep increasing when the UITableView scrolling

there is a strange problem I have not met ever
there is an array() including some custom object named MyClass parsed by JSONKit;
when I keep scrolling the tableview the memory will keeping increasing too.
but when replace
cell.textLabel.text = myclass.name;
with
cell.textLabel.text = #"cool";
or
cell.textLabel.text = [NSString stringWithFormate:#"a-%d", indexPath.row];
it's ok the memory with keep stable
but if I use
cell.textLabel.text = [NSString stringWithFormate:#"a-%#-i",myclass.name, indexPath.row];
it also keep increasing;
It will drive my crazy!!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Singers";
OMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
MyClass *myclass = [self.data objectAtIndex:indexPath.row];
if (cell == nil){
cell = [[[OMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.textLabel.text = myclass.name;
return cell;
}
MyClass
there is two class one Base another inherit
Base:
#interface OMBase : NSObject {
NSMutableDictionary *data;
NSString *name;
NSArray *keys;
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, copy) NSMutableDictionary *data;
#implementation OMBase
#synthesize data, name;
- (void)setData:(NSMutableDictionary *)adata{
if (data){
[data release];
data = nil;
}
data = [adata mutableCopy];
}
- (void)dealloc{
if (keys){
[keys release];
}
[data release];
[super dealloc];
}
- (id)init{
if (self = [super init]){
self.data = [[[NSMutableDictionary alloc] initWithCapacity:20] autorelease];
}
return self;
}
inherit:
#import "OMBase.h"
#interface OMLyric : OMBase
- (NSString *)songid;
- (NSString *)content;
#import "OMLyric.h"
#implementation OMLyric
- (NSString *)songid{
return [data objectForKey:#"songid"];
}
- (NSString *)content{
return [data objectForKey:#"content"];
}
Seems like your myclass.name getter returns a new allocated object. We can't say more without seeing myclass.

UITableView doesn't display Address Book

I think I'm not getting the handle on address book and UITableView, or maybe I'm just making it simpler than it is.
here is my .h file:
#interface Contacts : UITableViewController <ABPeoplePickerNavigationControllerDelegate> {
NSMutableArray *menuArray;
NSString *firstName;
NSString *lastName;
UIButton *addcontact;
}
#property (nonatomic, retain) NSMutableArray *menuArray;
#property (nonatomic, strong) UIButton *addcontact;
#property (nonatomic, strong) NSString *firstName;
#property (nonatomic, strong) NSString *lastName;
-(void)showPeoplePickerController;
#end
And my .m file:
-(IBAction)addcontact:(id)sender {
ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
[peoplePicker setPeoplePickerDelegate:self];
[self presentModalViewController:peoplePicker animated:YES]; }
- (void)viewDidLoad
{
menuArray = [[NSMutableArray alloc] init];
[super viewDidLoad];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
[menuArray addObject:firstName];
lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
[menuArray addObject:lastName];
[self dismissModalViewControllerAnimated:YES];
return NO;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [menuArray count];
}
- (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];
}
NSString *cellValue = [menuArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
I hope someone could help me with it, I'm fairley new to UITableViews and Addressbooks :)
For performance purposes (not to mention animation purposes), UITableView does not automatically update itself whenever the data source changes.
Try adding [myTableView reloadData]; after you add the first and last name to the arrays (or in viewWillAppear:

Resources