2015-08-15 21 views
-1

我對iOS很新,但如果我可以讓這個錯誤消失,我正在完成我的應用程序。我在C和C++方面經驗豐富,但Objective-C在我完成工作的方式上相當困惑。iOS:在NSMutableArray中使用未聲明的標識符

頭文件:

@interface ThirdTableViewController : UITableViewController<MFMailComposeViewControllerDelegate> 

-(id) init; 
@property (strong, nonatomic) NSMutableArray *csvFileNames; 
@property (strong, nonatomic) NSMutableArray *csvFilePaths; 
- (IBAction)refreshTableButton:(id)sender; 
- (IBAction)sendEmailButton:(id)sender; 
void refreshTable(); 

@end 

實現文件:

void refreshTable(){ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]; 

    NSArray *csvFiles = [documentArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedObject, NSDictionary *bindings) { 
     return [evaluatedObject hasSuffix:@".csv"]; 
    }]]; 

    _csvFileNames = csvFiles; 

    for (NSString *fileName in csvFiles) { 
     [_csvFilePaths addObject:[documentsDirectory stringByAppendingPathComponent:fileName]]; 
    } 

    //NSLog(@"files array %@", _fileNamesArray); 
    //NSLog(@"files array %@", _filePathsArray); 
} 

我得到我的地方在.h文件中聲明瞭兩個NSMutableArrays在.m文件中使用的錯誤。這些都是具體線路:

_csvFileNames = csvFiles; 

[_csvFilePaths addObject:[documentsDirectory stringByAppendingPathComponent:fileName]]; 

這些都是具體的錯誤:使用未聲明的標識符的「_csvFileNames」,使用未聲明的標識符的「_csvFilePaths」

在C++中,如果我們要實現一類功能,我們做就像class :: myfunction(parameters ...)。我認爲我的問題是沿着這些線路的某處。

+0

聲明稍後爲NSString的 - 如何做錯誤(不)的變化?使用它來改進標題並使其更準確地解決問題。 – user2864740

+1

用實際和完整的錯誤消息更新您的問題。 – rmaddy

+0

我會盡快做到這一點。 –

回答

2

問題出在您的refreshTable函數。這是一個功能,而不是實例方法。這樣的函數不能訪問類的任何實例方法或變量。

在.H,變化:

void refreshTable(); 

到:

- (void)refreshTable; 

更新.M:

void refreshTable(){ 

到:

- (void)refreshTable { 

然後在任何你稱呼它,改變:一旦你這樣做,你將有其他問題

[self refreshTable]; 

refreshTable(); 

來。您正嘗試將NSArray分配給NSMutableArray類型的變量。更改此代碼:

NSArray *csvFiles = [documentArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedObject, NSDictionary *bindings) { 
    return [evaluatedObject hasSuffix:@".csv"]; 
}]]; 

_csvFileNames = csvFiles; 

for (NSString *fileName in csvFiles) { 
    [_csvFilePaths addObject:[documentsDirectory stringByAppendingPathComponent:fileName]]; 
} 

到:

NSArray *csvFiles = [documentArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedObject, NSDictionary *bindings) { 
    return [evaluatedObject hasSuffix:@".csv"]; 
}]]; 

_csvFileNames = [NSMutableArray array]; 

for (NSString *fileName in csvFiles) { 
    [_csvFilePaths addObject:[documentsDirectory stringByAppendingPathComponent:fileName]]; 
} 

更重要的是,使用性能:

self.csvFileNames = [NSMutableArray array]; 

for (NSString *fileName in csvFiles) { 
    [self.csvFilePaths addObject:[documentsDirectory stringByAppendingPathComponent:fileName]]; 
} 
+0

謝謝。我很感激。這現在更有意義了。 –

-1

在您的refreshTable方法中,您將csvFiles聲明爲NSArray,並試圖將其指定給NSCreateArray的_csvFileNames。

您不能將變量NSArray分配給NSMutableArray變量。

+0

這是一個有效的問題,但由於問題中的錯誤的實際原因,OP尚未達成目標。一旦當前問題得到解決,接下來就需要解決這個問題。僅供參考 - 我沒有投票。 – rmaddy

+0

我很欣賞這個答覆。我高舉你希望擺脫誰downvoted –