簡體   English   中英

NSMutableArray返回null嗎?

[英]NSMutableArray returns null?

我有一個UITableView,它填充了我的NSMutableArray。 這就是我在.h中設置它的方式

@interface processViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *processList;
}

@property (copy, readwrite) NSMutableArray *processList;

和我的.m

@synthesize processList;

-(void)viewDidLoad {
    processList = [[NSMutableArray alloc] init];
}

我在viewDidLoad上放了一個NSLog,它顯示得很好。 但是在我執行操作后, processList數組返回null 有什么想法嗎?

謝謝
庫爾頓

編輯1:

- (void)startUploads {

// Start UIActivity in the top
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

// Start Pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

// Display results for testing purposes (commented out)
NSArray *resultstwo = [database executeQuery:@"SELECT * FROM processes"];
for (NSDictionary *rowtwo in resultstwo) {

    // Get ID
    int getUserIDcount = 0;
    NSArray *getUserIDInfo = [database executeQuery:@"SELECT * FROM login"];
    for (NSDictionary *getUserIDRow in getUserIDInfo) {
        getUserIDcount++;
        NSString *oneUserID = [getUserIDRow valueForKey:@"id"];
        theUserID = [NSString stringWithFormat:@"%@", oneUserID];
    }

    // If theUserID exists...
    if (getUserIDcount == 0) {
        //myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector: @selector(checkLogin) userInfo: nil repeats: NO];
    } else {


        // Get URL of image
        NSString *sqlImageUploadPathOne = @"./../Documents/";
        NSString *sqlImageUploadPathTwo = [rowtwo valueForKey:@"image"];
        NSString *getAlbumID = [rowtwo valueForKey:@"album"];
        NSString *sqlImageUploadPath = [NSString stringWithFormat:@"%@%@",sqlImageUploadPathOne,sqlImageUploadPathTwo];

        //testLabel.text = @"Uploading...";

        // Display Image in UIImageView (uploadImageHidden)
        UIImage *attemptImage = [UIImage imageNamed:sqlImageUploadPath];
        [uploadImageHidden setImage:attemptImage];



        // Upload to server
        NSData *imageData = UIImageJPEGRepresentation(uploadImageHidden.image, 90);
        NSString *urlStringOne = @"http://myflashpics.com/iphone_processes/upload.php?album=";
        NSString *urlStringTwo = @"&id=";
        NSString *urlString = [NSString stringWithFormat:@"%@%@%@%@",urlStringOne,getAlbumID,urlStringTwo,theUserID];

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];

        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        //NSLog(@"%@", returnString);

        if ([returnString rangeOfString:@"yes"].location == NSNotFound) {
            // Fail
        } else {
            // Delete image if successful
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
            NSString *documentsDirectoryPath = [paths objectAtIndex:0];
            NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:sqlImageUploadPathTwo];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager removeItemAtPath:myFilePath error:NULL];
            [database executeNonQuery:@"DELETE FROM processes WHERE image=?", sqlImageUploadPathTwo];

            // Get Photo ID
            NSArray *myWords = [returnString componentsSeparatedByString:@" "];
            NSString *photoID = [myWords objectAtIndex:1]; 
            NSString *usernameID = [myWords objectAtIndex:2]; 

            NSString *defaultName = @"Photo uploaded from the flashpics iPhone application";

            // Get Thumbnail URL
            NSString *thumbnailURLOne = @"http://myflashpics.com/users/";
            NSString *thumbnailURLTwo = @"/pictures/thumbnails/";
            NSString *thumbnailURLThree = @".jpg";
            NSString *thumbnailURL = [NSString stringWithFormat:@"%@%@%@%@%@",thumbnailURLOne,usernameID,thumbnailURLTwo,photoID,thumbnailURLThree];

            // Download thumbnail
            //NSLog(@"Downloading...");
            UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnailURL]]];
            //NSLog(@"%f,%f",image.size.width,image.size.height);
            NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            //NSLog(@"%@",docDir);
            //NSLog(@"saving jpeg");
            NSString *jpegFilePath = [NSString stringWithFormat:@"%@/%@_thumbnail.jpg",docDir,photoID];
            NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.2f)];//1.0f = 100% quality
            [data2 writeToFile:jpegFilePath atomically:YES];
            //NSLog(@"saving image done");
            [image release];

            // Put in database
            NSString *thumbnailEnd = @"_thumbnail.jpg";
            NSString *thumbnailLocation = [NSString stringWithFormat:@"%@%@",photoID,thumbnailEnd];

            int theCount = 0;
            NSArray *getUserIDInfotoo = [database executeQuery:@"SELECT * FROM images WHERE id=?",photoID];
            for (NSDictionary *getUserIDRowtoo in getUserIDInfotoo) {
                theCount++;
            }

            if (theCount == 0) {
                [database executeNonQuery:@"INSERT INTO images (id, name, thumbnail, album) VALUES (?, ?, ?, ?)", photoID, defaultName, thumbnailLocation, getAlbumID];
            }
            //[NSThread detachNewThreadSelector:@selector(updateImages) toTarget:RootViewController withObject:nil];
            //myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector: @selector(updateImages) userInfo: nil repeats: NO];
        }

        [request release];
        [returnString release];

    }

    //NSLog([rowtwo valueForKey:@"image"]);
    //NSLog([rowtwo valueForKey:@"album"]);
}


[pool release];

// Stop the UIActivity in the top bar
TableViewAppDelegate *dataCeter = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
if ([dataCeter.dataTen isEqualToString:@""]) {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}

編輯2:

調用startUploads的位置(不同的.m)

processViewController *processTable = [[processViewController alloc] initWithNibName:@"processView.xib" bundle:nil];
[processTable startUploads];
[processTable release];

更多代碼會有所幫助,但是這里有一些建議:

  1. 在加載視圖之前,檢查是否正在調用startUploads 僅在首次訪問該視圖並將其添加到超級視圖時,才會加載該視圖。

  2. 考慮在init方法中初始化processList而不是viewDidLoad來解決#1和b / c問題,iOS可以獨立於viewController的生命周期來加載和卸載視圖(取決於要顯示的其他視圖以及是否發生任何內存警告) 。

  3. 請確保您釋放processListdealloc 如果在viewDidLoad重新創建並加載了它,則只需在viewDidUnload釋放它。

當您的代碼示例不顯示startUploads被調用,您不添加任何產品processList所以這是很難說,如果上面是相關的。 發布更多代碼,我將相應地修改答案。

祝好運!

[編輯:添加示例代碼]

您發布的代碼片段不是視圖控制器及其交互對象的完整實現。 給定我所看到的代碼,您的應用程序設計不符合MVC(模型/視圖/控制器)設計模式,我的工作方式將有所不同。 但是,我不想對我從未見過的代碼或您作為開發人員的最終意圖或能力進行假設。 我無法為您編寫應用程序,只是嘗試直接幫助您解決有關在startUploads操作完成后您的NSMutableArray屬性為何仍為null的特定問題。 考慮到這一點,這是我對您發布的代碼的編輯:

processViewController.m-添加以下內容:

- (id)initWithNibNamed:(NSString *)nibName bundle:(NSBundle *)bundle {
    self = [super initWithNibNamed:nibName bundle:bundle];
    if (self) {
        processList = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc {
    self.processList = nil;
    [super dealloc];
}

different.m

- (void)displayProcessVC {
    ProcessViewController *processVC = [[ProcessViewController alloc] initWithNibNamed:@"processView.xib" bundle:nil];
    NSLog(@"Different:displayProcessVC BEFORE STARTING UPLOAD, processList = %@", processVC.processList);
    [processVC startUploads];
    NSLog(@"Different:displayProcessVC AFTER STARTING UPLOAD, processList = %@", processVC.processList);
    // would normally present process VC here
    [processVC release];
}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM