简体   繁体   English

无法从NSMutableArray(IPHONE SDK)获取自定义对象

[英]Can't get custom object from NSMutableArray (IPHONE SDK)

I have a problem with a NSMutableArray... After I fill the NSMutableArray in my function (readRoutes) I can't extract the objects.... Inside the readRoutes function I can... I can't figure where is the problem.. 我的NSMutableArray有问题...在我的函数(readRoutes)中填充了NSMutableArray之后,我无法提取对象....在readRoutes函数中,我可以...我不知道问题出在哪里..

First the NSMutableArray is fill with Route objects in the readRoutes method...(Inside RoutesListView file) (I need to fill after TableView cells with these Route objects...) 首先,NSMutableArray在readRoutes方法中填充Route对象...(在RoutesListView文件内部)(我需要在TableView单元格之后填充这些Route对象...)

This is the Route.h file : 这是Route.h文件:

@interface Route : NSObject {
    NSString *routeId;
    NSString *routeDirection;
    NSString *routeName;
    NSString *routeCode;
}
@property (nonatomic,retain) NSString *routeId;
@property (nonatomic,retain) NSString *routeDirection;
@property (nonatomic,retain) NSString *routeName;
@property (nonatomic,retain) NSString *routeCode;

-(id)initWithName:(NSString *)name direction:(NSString *)d routeId:(NSString *)rid code:(NSString *)c;
@end

The Route.m file: Route.m文件:

#import "Route.h"

@implementation Route
@synthesize routeId;
@synthesize routeDirection;
@synthesize routeName;
@synthesize routeCode;

-(id)initWithName:(NSString *)name direction:(NSString *)d routeId:(NSString *)rid code:(NSString *)c{
    self.routeId=rid;
    self.routeName=name;
    self.routeDirection=d;
    self.routeCode=c;
    return self;
}
@end

Now this is the RoutesListView.h file (a UITableViewController) 现在这是RoutesListView.h文件(一个UITableViewController)

#import <UIKit/UIKit.h>
#import "Route.h"

@interface RoutesListView : UITableViewController  <UITableViewDelegate,UITableViewDataSource> {
    NSMutableArray *routeArray;
    NSString *dbName;
    NSString *dbPath;
}
@property (nonatomic,retain) NSMutableArray *routeArray;
@property (nonatomic,retain) NSString *dbName;
@property (nonatomic,retain) NSString *dbPath;

-(void)readRoutes;
@end

Inside the RoutesListView.m file : 在RoutesListView.m文件中:

#import "RoutesListView.h"
#import "Route.h"
#import <sqlite3.h>

@implementation RoutesListView

@synthesize routeArray;
@synthesize dbName,dbPath;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    dbName = @"data.db";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    dbPath = [documentsDir stringByAppendingPathComponent:dbName];
    routeId =[[NSMutableArray alloc] init];

    //routeArray

        NSMutableArray * array = [[NSMutableArray alloc] init]; 
    self.routeArray = array;


    [array release];
    [self readRoutes];
    [super viewDidLoad];
}

INSIDE THE readRoutes method when I fill the NSMutableArray (The method work fine...): 当我填充NSMutableArray时,将其放在readRoutes方法中(该方法可以正常工作...):

while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

                // Read the data from the result row
                NSString *aId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                NSString *aDirection =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                NSString *aCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                Route *maRoute = [[Route alloc] initWithName:aName direction:aDirection routeId:aId code:aCode];

                                // I fill the NSMutableArray here...
                [routeArray addObject:maRoute];

                                [maRoute release];
                [aId release];
                [aDirection release];
                [aName release];
                [aCode release];
            }

FINALLY WHEN I SET THE CELL TEXTLABEL...IT CRASH! 最终,当我设置了单元课本时……崩溃了! :

- (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...
    NSUInteger row = [indexPath row];
    Route *myRoute = (Route *)[routeArray objectAtIndex:row];
    cell.textLabel.text = myRoute.routeName;
    [myRoute release];
    return cell;
}

Help me to understand... I lost my hair on that... 帮助我了解...我为此失去了头发...

Thanks 谢谢

Maxime 马克西姆

You are over releasing several objects which will usually cause your application to crash. 您已经释放了多个对象,这些对象通常会导致应用程序崩溃。 Also your init function is not properly implemented inside of your Route.m file. 另外,您的init函数未在Route.m文件内部正确实现。 Make the following changes. 进行以下更改。

#import "Route.h"

@implementation Route
@synthesize routeId;
@synthesize routeDirection;
@synthesize routeName;
@synthesize routeCode;

-(id)initWithName:(NSString *)name direction:(NSString *)d routeId:(NSString *)rid code:(NSString *)c{
    //Must call supers implementation of init
    self = [super init];
    if(self)
    {
        self.routeId=rid;
        self.routeName=name;
        self.routeDirection=d;
        self.routeCode=c;
    }
    return self;
}
@end

Next you are over releasing objects inside of readRoutes 接下来,您将要释放readRoutes内部的对象

while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
    // Read the data from the result row
    NSString *aId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
    NSString *aDirection =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
    NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
    NSString *aCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

    Route *maRoute = [[Route alloc] initWithName:aName direction:aDirection routeId:aId code:aCode];

    // I fill the NSMutableArray here...
    [routeArray addObject:maRoute];

    [maRoute release]; //This is right you called alloc/init
    [aId release];       //This is wrong never retained,copy or alloc/init
    [aDirection release];//Dont release here either
    [aName release];     //Dont release
    [aCode release];     //Dont release
}

And finally you are over releasing the route inside of the following code 最后,您在以下代码中释放了路由

- (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...
    NSUInteger row = [indexPath row];
    Route *myRoute = (Route *)[routeArray objectAtIndex:row];
    cell.textLabel.text = myRoute.routeName;

    [myRoute release]; //Do not release route here

    return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iPhone Obj-C - 从NSMutableArray中检索自定义对象 - iPhone Obj-C - Retrieve custom object from NSMutableArray 在nsmutableArray iPhone SDK中访问对象的属性 - Accessing properties of object in an nsmutablearray iphone sdk 无法在iPhone中的nsmutablearray中获取对象的索引值 - Unable to get an indexvalue of an object in nsmutablearray in iphone 从地址簿中获取图像并将其存储到 NSmutablearray 但在显示它时会在 iphone sdk 中出现“EXCBAD 错误”? - get image from address book and stored it to NSmutablearray but while showing it gives `EXCBAD Error` in iphone sdk? 如何从iPhone中的NSMutableArray获取联系人记录 - how to get records of contacts from NSMutableArray in iPhone 如何从NSMutableArray(iPhone)获得第一个值? - How to get first value from NSMutableArray (iPhone)? 从方法中返回NSMutableArray ...我之前是否发布它? - iPhone SDK - Returning an NSMutableArray from a method…do I release it prior? - iPhone SDK 从NSMutableArray iPhone SDK删除多个对象时出现问题 - Problem removing multiple objects from NSMutableArray iPhone SDK iPhone SDK:NSMutableArray无法识别的选择器 - iPhone SDK: NSMutableArray unrecognized selector iPhone-自定义对象内的NSMutableArray。 什么时候发布? - iPhone - NSMutableArray inside a Custom Object. When to release?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM