![](/img/trans.png)
[英]How to copy the contents of NSMutableArray to another NStArray in iphone app?
[英]How to copy NSMutableArray value into another NSMutableArray in iPhone?
在這里,我的AppDelegate
頁面中有一個NSMutableArray
,當用戶在數據庫中添加值(用戶名)時,該數組將更新。 現在我希望在下一個視圖中顯示數組值(用戶名),以在Picker視圖中列出。 我知道還有更多相關問題,但是我找不到解決方案。 任何幫助表示贊賞。
這是我的代碼:
我在AppDelegate
頁面中聲明的數組名為jobarray
,現在我需要將jobarray
的項目復制到下一個視圖中,在那里我聲明了pickerarray
。
pickerarray = [[NSMutableArray alloc] initWithArray:appDelegate.jobarray copyItems:YES];
但它返回錯誤消息,
['NSInvalidArgumentException', reason: '-[Coffee copyWithZone:]: unrecognized selector sent to instance 0x822d410'] where 'Coffee' is my sqlite3 object file.
您的SQLite對象不符合NSCopying協議-您尚未實現
- (id)copyWithZone:(NSZone *)zone;
方法。 您可以實現此方法以使對象可復制,但是如果只需要復制以顯示對象,則最好僅復制數組本身 :
NSMutableArray *newArray = [originalArray mutableCopy];
這僅保留第一個數組的對象,而無需弄亂實現復制協議。
[arr1 initWithObjects:@"1",@"2", nil];
毫無意義,您可能想要使用NSMutableArray:
NSArray *arr1 = [NSArray arrayWithObjects:@"1",@"2", nil];
NSMutableArray *arr2 = [arr1 mutableCopy];
NSLog(@"arr1: %p, %@", arr1, arr1);
NSLog(@"arr2: %p, %@", arr2, arr2);
NSLog輸出:
arr1: 0x7924c50, (
1,
2
)
arr2: 0x7924f00, (
1,
2
)
要從數組填充UIPickerView,您需要首先為UIPickeView數據源創建一個連接,然后再委托給ViewController。 您可以通過按正常方式CTRL從UIPickerView控件中拖入Interface Builder來完成此操作。
您還需要確保將代理協議包括在接口文件中。 像這樣
@ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> {
}
然后,您將需要在實現文件中為UIPickerView的填充創建委托方法。
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return myArrayObject.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//set item per row
return [myArrayObject objectAtIndex:row];
}
假定myArrayObject已正確聲明並填充到其他位置。 即在viewDidLoad中-如果內容是靜態的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.