trying to copy array from one class to array but copied array showing null - iphone

Hi, I am trying to get array from StudentDbwithsearchbarViewController class to SearchBarDB class but resultant array not having any data it is giving null Array. Please help me out with this.
thanks in advance
#import <UIKit/UIKit.h>
#interface StudentDbwithsearchbarViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITextField *txtMarks,*txtSname;
IBOutlet UITableView *tableStudents;
NSMutableArray *arrStudents;
}
#property(nonatomic,retain) NSMutableArray *arrStudents;
-(IBAction)saveStudentDetails;
-(IBAction)gotoSearchpage;
#end
#implementation StudentDbwithsearchbarViewController
#synthesize arrStudents;
- (void)viewDidLoad {
[super viewDidLoad];
arrStudents = [[DbStudent getStudentRecords]retain];
NSLog(#"%#",arrStudents);
NSLog(#"%d",[arrStudents retainCount]);
}
#import "DbStudent.h"
+(NSMutableArray*)getStudentRecords{
NSArray *arrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDestPath = [NSString stringWithFormat:#"%#/Student5.sqlite",[arrDocPath objectAtIndex:0]];
NSMutableArray *arrStudents = [[NSMutableArray alloc]init];
sqlite3 *db;
if(sqlite3_open([strDestPath UTF8String], &db)==SQLITE_OK)
{
NSString *query = #"SELECT * FROM Student";
void* v;
char* err_msg;
sqlite3_stmt *studentStmt;
if(sqlite3_prepare_v2(db, [query UTF8String], -1, &studentStmt, &err_msg)==SQLITE_OK)
{
while (sqlite3_step(studentStmt)==SQLITE_ROW) {
int sno = sqlite3_column_int(studentStmt, 0);
NSString *sname = [NSString stringWithUTF8String: sqlite3_column_text(studentStmt, 1)];
float marks = sqlite3_column_double(studentStmt, 2);
Student *st = [[Student alloc]init];
st.Sno = sno;
st.Sname = sname;
st.marks = marks;
[arrStudents addObject:st];
}
}
}
return arrStudents;
}
#import "SearchBarDB.h"
#import"StudentDbwithsearchbarViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
StudentDbwithsearchbarViewController *sbd = [[StudentDbwithsearchbarViewController alloc]init];
NSLog(#"%d",[sbd.arrStudents retainCount]);
NSLog(#"%#",sbd.arrStudents);
// arrstudentBase = [sbd.arrStudents copy];
arrMatchedString = [[NSMutableArray alloc]init];
}

Lots of potential problems, no definitive answer without a clue as to what you've tried or how you've determined that it failed.
methods should not be prefixed with get; just call it studentRecords or fetchStudentRecords
your memory management code is all over the place; you'll be leaking that array, at the least.
retainCount is useless, don't call it.
writing raw SQL is a waste of time; at least use a wrapper like FMDB or, better yet, move to CoreData
Best guess for failure: the database doesn't exist or the query fails. Have you stepped through the query code?

Related

writing data using NSKeyedArchiver

I trying to learn how to save array of objects using NSKeyedArchiver and I coded a small application to do that and I logged to see if the array was saved but everytime I get 0 for array count and here is the code.
ViewController.h
#interface ViewController : UIViewController
{
IBOutlet UITextField *text;
IBOutlet UITextField *textName;
IBOutlet UITextField *textAge;
IBOutlet UILabel *name;
IBOutlet UILabel *age;
BOOL flag;
BOOL choice;
NSString *documentDirectory;
NSMutableArray *anArray;
Person *p;
NSData *data;
}
-(BOOL) dataFilePath;
-(IBAction)readPlist;
-(IBAction) writePlist;
#property (strong,nonatomic)IBOutlet UITextField *text;
#property (strong,nonatomic)IBOutlet UITextField *textName;
#property (strong,nonatomic)IBOutlet UITextField *textAge;
#property (strong,nonatomic)IBOutlet UILabel *name;
#property (strong,nonatomic)IBOutlet UILabel *age;
#property (strong,nonatomic)NSString *documentDirectory;
#property (strong,nonatomic)NSMutableArray *anArray;
#end
ViewController.m
#interface ViewController ()
#end
#implementation ViewController
#synthesize text,documentDirectory,textAge,textName,name,age,anArray;
- (void)viewDidLoad
{
[super viewDidLoad];
// checking if the file was created and show a message if its created or not.
if ([self dataFilePath]) {
NSLog(#"File Created !");
} else {
NSLog(#"File Not Created !");
}
NSLog(#"File location : %#",documentDirectory);
choice = YES;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL) dataFilePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentDirectory = [path objectAtIndex:0];
documentDirectory = [documentDirectory stringByAppendingPathComponent:#"MilmersĀ­Data.dat"];
return TRUE;
}
- (IBAction)writePlist
{
p.name = textName.text;
p.age = [textAge.text intValue];
[anArray addObject:p];
for (int i=0; i<[anArray count]+1; i++) {
Person *pp = [[Person alloc]init];
pp=[anArray objectAtIndex:i];
NSLog(#"Name: %#",pp.name); // checking the names in pp object but getting null
}
data = [NSKeyedArchiver archivedDataWithRootObject:anArray];
[data writeToFile:documentDirectory options:NSDataWritingAtomic error:nil];
NSLog(#"Array length: %d",[anArray count]); //Always got array count zero.
}
-(IBAction)readPlist
{
NSString *filePath = documentDirectory;
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(#"The array is: %#",array); // it shows that there is nothing in the array
}
#end
I wrote the class for writing .plist files originally but I knew later that I cant store objects in .plist file so I tried so that with archive, thats why the method name have plist in it.
Thank you in advance
Looks like you aren't ever creating an instance of p to add to the array. Try:
Person *p = [[Person alloc] init];
p.name = textName.text;
p.age = [textAge.text intValue];
[anArray addObject:p];
your index limit was also wrong in this loop
for (int i=0; i<[anArray count]; i++) {
NSLog(#"Name: %#", [[anArray objectAtIndex:i] name]);
}
you should really have been seeing a couple of different crashes...
Try adding this in viewDidLoad
[[NSFileManager defaultManager] createFileAtPath:documentDirectory contents:nil error:nil];
It looks like you never do this, and using archives to write to files only works if the file already exists (make sure you only do this once, otherwise every time that view is loaded the file will be emptied of all the data in it). And when you do this
if ([self dataFilePath])
It's pointless, because no matter what it always returns yes, whether the file exists or not.
Does your Person class implement NSCoding?
Specifically you need to implement something like the following in Person.m:
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) {
return nil;
}
self.name = [decoder decodeObjectForKey:#"name"];
self.age = [decoder decodeObjectForKey:#"age"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.name forKey:#"name"];
[encoder encodeObject:self.age forKey:#"age"];
}

-[EngineerModel _isNaturallyRTL]: unrecognized selector sent to instance

Getting this error:
'NSInvalidArgumentException', reason: '-[EngineerModel _isNaturallyRTL]: unrecognized selector sent to instance
I've read How to resolve 'unrecognized selector sent to instance'? and others.
Using Xcode 4.5.1 with arc (my first time with arc)
Here's my code, which is based on an earlier non-arc project which works great
database.m
// Models for data
#import "EngineerModel.h"
- (NSArray *)returnEngineers
{
NSMutableArray *retval = [[NSMutableArray alloc] init];
NSString *query = #"SELECT * FROM engineers";
stmt = nil;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &stmt, nil) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
char *engineerIDChr = (char *) sqlite3_column_text(stmt, 0);
char *engineerNameChr = (char *) sqlite3_column_text(stmt, 1);
char *engineerSigFileChr = (char *) sqlite3_column_text(stmt, 2);
char *engineerPhoneChr = (char *) sqlite3_column_text(stmt, 3);
char *engineerEmailChr = (char *) sqlite3_column_text(stmt, 4);
char *engineerRegNoChr = (char *) sqlite3_column_text(stmt, 5);
NSString *engineerID = [[NSString alloc] initWithUTF8String:engineerIDChr];
NSString *engineerName = [[NSString alloc] initWithUTF8String:engineerNameChr];
NSString *engineerSigFile = [[NSString alloc] initWithUTF8String:engineerSigFileChr];
NSString *engineerPhone = [[NSString alloc] initWithUTF8String:engineerPhoneChr];
NSString *engineerEmail = [[NSString alloc] initWithUTF8String:engineerEmailChr];
NSString *engineerRegNo = [[NSString alloc] initWithUTF8String:engineerRegNoChr];
EngineerModel *info = [[EngineerModel alloc] initWithUniqueId:engineerID
engineerName:engineerName
engineerSigFile:engineerSigFile
engineerPhone:engineerPhone
engineerEmail:engineerEmail
engineerRegNo:engineerRegNo];
[retval addObject:info];
}
sqlite3_finalize(stmt);
}
return retval;
}
Database has two entries
And the model
// EngineerModel.h
#import <Foundation/Foundation.h>
#interface EngineerModel : NSObject
{
NSString *_engineerID;
NSString *_engineerName;
NSString *_engineerSigFile;
NSString *_engineerPhone;
NSString *_engineerEmail;
NSString *_engineerRegNo;
}
#property (nonatomic, copy) NSString *engineerID;
#property (nonatomic, copy) NSString *engineerName;
#property (nonatomic, copy) NSString *engineerSigFile;
#property (nonatomic, copy) NSString *engineerPhone;
#property (nonatomic, copy) NSString *engineerEmail;
#property (nonatomic, copy) NSString *engineerRegNo;
- (id)initWithUniqueId:(NSString *)AengineerID
engineerName:(NSString *)AengineerName
engineerSigFile:(NSString *)AengineerSigFile
engineerPhone:(NSString *)AengineerPhone
engineerEmail:(NSString *)AengineerEmail
engineerRegNo:(NSString *)AengineerRegNo;
- (id) init;
#end
// EngineerModel.m
#import "EngineerModel.h"
#interface EngineerModel ()
#end
#implementation EngineerModel
#synthesize engineerID, engineerName, engineerSigFile, engineerPhone, engineerEmail, engineerRegNo;
- (id)initWithUniqueId:(NSString *)AengineerID
engineerName:(NSString *)AengineerName
engineerSigFile:(NSString *)AengineerSigFile
engineerPhone:(NSString *)AengineerPhone
engineerEmail:(NSString *)AengineerEmail
engineerRegNo:(NSString *)AengineerRegNo
{
if ((self = [super init]))
{
self.engineerID = AengineerID;
self.engineerName = AengineerName;
self.engineerSigFile = AengineerSigFile;
self.engineerPhone = AengineerPhone;
self.engineerEmail = AengineerEmail;
self.engineerRegNo = AengineerRegNo;
}
return self;
}
- (id) init {
self = [super init];
return self;
}
#end
Lastly
I've added -ObjC and -all_load to other linker flags
Added #synthesize (I didn't think I had to for arc?)
If I simplify it down to
- (id)initWithId:(NSString *)AengineerID
{
NSLog(#"AengineerID %#",AengineerID);
if ((self = [super init]))
{
self.engineerID = AengineerID;
}
return self;
}
It traces AengineerID then crashes
Any ideas?
_isNaturallyRTL is a private method on NSString. So somehow you've got an EngineerModel instance where some other code is expecting an NSString.
What do you do with that array of EngineerModel objects you return from the returnEngineers method?
And just as a recommendation... using the sqlite3 C API yourself like that is a lesson in frustration. I highly recommend using something like FMDB instead.

Having problems with Array

SO here's my setup. I have an object called radiostations where I have several strings like callsign, frequency declared and an NSMutableArray called amStationInfo. On my viewcontroller, I access an SQLite database which populates the an array like so...
radiostations.h
#interface radiostations : NSObject {
NSString *format;
NSString *city;
}
#property (nonatomic, retain) NSString *format;
#property (nonatomic, retain) NSString *city;
ViewController.m
radiostations *amStationClass = [[radiostations alloc] init];
NSMutableArray* amStationInfo = [[NSMutableArray alloc] init];
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *cityField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 10)];
NSString *formatField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[amStationInfo addObject:amStationClass];
[amStationClass setCity:cityField];
[amStationClass setFormat:formatField];
}
[tabView reloadData];
sqlite3_finalize(statement);
and then I populate a UITableView
NSString *cityValue = [(radiostations *)[amStationInfo objectAtIndex:indexPath.row] city];
NSString *formatValue = [(radiostations *)[amStationInfo objectAtIndex:indexPath.row] format];
cityLabel.text = cityValue;
formatLabel.text = formatValue;
Initially I was dealing with a few Arrays and this worked just fine. I then changed it so that I was only dealing with one array using a class object and now it's not working. I know the SQLite query and what not works so Im not having any problems with that. It seems as though the array does not get populated.
You are changing the properties of the same radiostations object and adding it over and over again to the array. You need to create a new radiostations object for each row from your sqlite database and add this:
while (...) {
// fetch data as before
radiostations *record = [[radiostations alloc] init];
[record setCity: cityField];
[record setFormat: formatField];
[amStationInfo addObject: record];
[record release];
}
If you are using ARC you need to remove the line [record release];, otherwise it is necessary to avoid leaking those objects.
where did you allocate/init your mutablearray?
something like:
NSMutableArray* amStationInfo = [[NSMutableArray alloc] init];
you need to allocate it once, before to add objects in it

return a static const []

So in my model I have the following code... I am successfully able to return each individual value. I want to know how am I able to return the entire speakerTable []... Maybe some advice. Thanks!
typedef struct {
NSUInteger speakerID;
NSString * speakerName;
NSString * speakerPosition;
NSString * speakerCompany;
} SpeakerEntry;
static const SpeakerEntry speakerTable [] =
{
{0, #"name", #"position", #"company"},
{1, #"name", #"position", #"company"},
{-1, nil, nil, nil}
};
This works correctly...
-(NSString *) stringSpeakerCompanyForId:(NSUInteger) identifier{
NSString * returnString = nil;
if ([self helpCount] > identifier) {
returnString = speakerTable[identifier].speakerCompany;
}
return returnString;
}
This does not work at all..
-(id) getSpeaker{
//if ([speakerTable[0].speakerName isKindOfClass:[NSString class]])
// NSLog(#"YES");
NSArray * myArray3 = [NSArray arrayWithArray:speakerTable];
return myArray3;
}
arrayWithArray expects an NSArray, not a C array.
The first one works because you are using it like a C array.
Alternatively - don't use a struct, use an object instead:
Create a class called Speaker.
In Speaker.h
#interface Speaker : NSObject {}
#property (nonatomic, assign) NSUinteger id;
#property (nonatomic, copy) NSString name;
#property (nonatomic, copy) NSString position;
#property (nonatomic, copy) NSString company;
- (void)initWithId:(NSUInteger)anId name:(NSString *)aName position:(NSString *)aPosition company:(NSString *)aCompany;
#end
in Speaker.m
#import "Speaker.h"
#implementation Speaker
#synthesize id, name, position, company;
- (void)initWithId:(NSUInteger)anId name:(NSString *)aName position:(NSString *)aPosition company:(NSString *)aCompany {
if (!([super init])) {
return nil;
}
id = anId;
NSString name = [[NSString alloc] initWithString:aName];
NSString position = [[NSString alloc] initWithString:aPosition];
NSString company = [[NSString alloc] initWithString:aCompany];
return self;
}
- (void)dealloc {
[name release];
[position release];
[company release];
[super dealloc];
}
#end
And now in your calling code you can create an immutable array of speakers with:
Speaker *speaker0 = [[Speaker alloc] initWithId:0 name:#"name0" position:#"position0" company:#"company0"];
Speaker *speaker1 = [[Speaker alloc] initWithId:1 name:#"name1" position:#"position1" company:#"company1"];
Speaker *speakerNull = [[Speaker alloc] initWithId:-1 name:nil position:nil company:nil];
NSArray *speakerArray [[NSArray arrayWithObjects: speaker0, speaker1, speakerNull] retain]
[speaker0 release];
[speaker1 release];
[speakerNull release];
note: this is typed straight in, so feel free to mention/correct typos or errors
The method arrayWithArray takes in an NSArray as an argument, not a C array.

iphone sqlite memory leak with [NSString stringWithUTF8String:(char*)sqlite3_column_text

I keep getting a memory leak indication from this sql statement when I assign the value retrieved from the database...
Person *tmpPerson = [[Person alloc] init];
tmpPerson.personName = [NSString stringWithUTF8String: (char*)sqlite3_column_text(SelectPersonStmt, 0)];
tmpPerson.personEmail = [NSString stringWithUTF8String: (char*)sqlite3_column_text(SelectPersonStmt, 1)];
[personList addObject:tmpPerson];
[tmpPerson release];
However if i replace the nsobject class object ...tmpPerson with regular NSString's ...leaks doesn't complain anymore? Does anyone know why?
NSString * personName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(SelectPersonStmt, 0)];
NSString * personEmail = [NSString stringWithUTF8String:(char*)sqlite3_column_text(SelectPersonStmt, 1)];
Here is my class definition ... is there anything wrong with it?
#interface Person : NSObject {
NSString* personName;
NSString* personMobile;
NSString* personEmail;
}
#property (nonatomic, retain) NSString* personName, *personEmail, *personMobile;
- (id)init
{
if ((self = [super init])) {
personName = [NSString string];
personEmail = [NSString string];
personMobile = [NSString string];
}
return self;
}
Am I missing something here ? Should I be even initializing these strings, it didn't seem to make any difference? I put them there incase i wanted to initialize them with some default value.
While testing this through instruments, i noticed that the memory leak is triggered during the deallocation method. I tried this and it didn't help either
-(void) dealloc
{
personName = nil;
personEmail = nil;
[super dealloc];
}
Any help would be greatly appreciated. I've seen a lot of posts related to this but I'm not sure if folks are getting the same behavior I have mentioned.
You have to release your ivar in the dealloc:
-(void) dealloc
{
[personName release];
[personEmail release];
[personMobile release];
personName = nil; // Optionnal
personEmail = nil; // Optionnal
personMobile = nil; // Optionnal
[super dealloc];
}
You should release the used strings in the Person struct, not setting it to NULL.
Once you set it to NULL and there are no other objects referring to it, you have a leak, the system does not know how to reclaim it.
EDIT: damn, my answer came 10 seconds late :P