我使用plist文件来获取显示在tableview中的站点列表
plist看起来像这样:
<array>
<dict>
<key>site</key>
<string>http://yahoo.com</string>
<key>title</key>
<string>Yahoo</string>
</dict>
<dict>
<key>site</key>
<string>http://google.com</string>
<key>title</key>
<string>Google</string>
</dict>
//...etc
</array>
</plist>
Run Code Online (Sandbox Code Playgroud)
我没有问题地显示:
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TestData" ofType:@"plist"]];
[self setContentsList:array];
Run Code Online (Sandbox Code Playgroud)
*问题是当我尝试搜索内容并且我想从搜索结果中获取valueforkey @"site"以在didSelectRowAtIndexPath中使用它:*
NSMutableArray *contentsList;
NSMutableArray *searchResults;
NSString *savedSearchTerm;
---------------------
- (void)handleSearchForTerm:(NSString *)searchTerm
{
[self setSavedSearchTerm:searchTerm];
if ([self searchResults] == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[self setSearchResults:array];
[array release], array = nil;
}
[[self searchResults] removeAllObjects];
if ([[self savedSearchTerm] length] != 0)
{
for (NSString *currentString in …Run Code Online (Sandbox Code Playgroud) iphone xcode uiwebview nsmutablearray uisearchdisplaycontroller
[_noteTitles addObject:@"title"];
[_noteDescriptions addObject:@"description"];
[self.tableView reloadData];
NSLog(@"%@", [_noteDescriptions description]);
NSLog(@"%@", [_noteTitles description]);
Run Code Online (Sandbox Code Playgroud)
在这些行之前,我合成了"_noteDescription","_ noteTitles".当调用NSLog行时,在控制台上我得到两个数组的"(null)".这个函数将被多次调用(不经常)来更新表的内容,但是现在数组没有被填充.
我在我的类的标题中声明了一个MutableArray rqst_entries作为属性并合成它.在viewdidload中我添加了以下代码:
self.rqst_entries = [NSMutableArray array];
Run Code Online (Sandbox Code Playgroud)
它是否会将此ivar变为自动释放并且可能更早发布(在调用dealloc之前)
感谢帮助,
斯特凡
我有一个UISwitch,我需要在将开关状态更改为OFF到ON时将字符串中的值加载到数组中.我使用了以下代码,但没有工作.
-(IBAction)toggleSwitch1:(id)sender {
if (addSwitch.on) {
[addSwitch addTarget:self action:@selector(addName:) forControlEvents:UIControlEventValueChanged];
NSLog(@"%@",userName);
}else {
NSLog(@"No");
}
}
-(void)addName:(UIControl*)sender {
[alertList addObject:userName];
NSLog(@"AlertList: %@", alertList);
}
Run Code Online (Sandbox Code Playgroud)
如果状态变为ON,如果我重新加载页面,我怎么能保持相同的状态呢?
请分享你的想法.谢谢
码:
LotteryEntry.h
#import <Foundation/Foundation.h>
@interface LotteryEntry : NSObject
{
NSCalendarDate *entryDate;
int firstNumber;
int secondNumber;
}
- (void) prepareRandomNumbers;
- (void) setEntryDate:(NSCalendarDate *)date;
- (NSCalendarDate*)entryDate;
- (int) firstNumber;
- (int) secondNumber;
@end
Run Code Online (Sandbox Code Playgroud)
LotteryEntry.m
#import "LotteryEntry.h"
@implementation LotteryEntry
- (void) prepareRandomNumbers
{
firstNumber = random() % 100 + 1;
secondNumber = random() % 100 + 1;
}
- (void) setEntryDate:(NSCalendarDate *)date
{
entryDate = date;
}
- (NSCalendarDate *) entryDate
{
return entryDate;
}
- (int)firstNumber
{
return firstNumber;
}
- …Run Code Online (Sandbox Code Playgroud) 我有一个NSMutablearray包含NSArrays(每个数组包含int和String值).当我尝试从第一个数组中检索并显示数据时:对于int值,这是正常的,它已正确显示.
NSLog(@"%i",[[[lesQuestions objectAtIndex:0] objectAtIndex:0] intValue]);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试显示String值时:
NSLog(@"%@",[[[lesQuestions objectAtIndex:0] objectAtIndex:1] stringValue]);
Run Code Online (Sandbox Code Playgroud)
我有例外:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString stringValue]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)
我绝对肯定int值是第一项(索引0)而String值是第二项(索引1).当我记录持有NSArrays的MutableArray时,我得到了正确的值,所以问题在于我猜的项目的引用.
这是一个用C++创建的简单类,用于为iOS设备创建的音乐应用程序,它将存储一些音符值及其时间:
class info {
public:
float attackTime;
Note noteStriked;
void setData(float timeOfAttack, Note nameOfStrikeNote){
attackTime = timeOfAttack;
noteStriked = nameOfStrikeNote;
}
};
Run Code Online (Sandbox Code Playgroud)
以上...注意是一个只能包含{SNARE,DRUM,HIHAT}等默认值的结构.我们的想法是创建一个Note对象并将这些对象存储在NSMutableArray中以供以后访问.
然后在我的主.h文件中,我有一个NSMutableArray sequenceOfNotes; 在我的.m文件中,我试图将一个对象添加到我的mutablearray ...但我不知道该怎么做.我尝试了各种各样的东西,但失败了它不起作用!
//Create one instance of the class
NoteData *currentNoteData;
// Update the instance of the class.. its two variables: attackTime and noteStriked
currentNoteData->attackTime = timeHit;
currentNoteData->noteStriked = SNARE;
//Then im trying to add the above instance to my mutableArray below
[sequenceOfNotes addObject:currentNoteData];
Run Code Online (Sandbox Code Playgroud)
在该行上产生的错误是无法初始化类型为'id'的参数,其值为'NoteData*'的左值
修复错误之后喜欢做的是能够在我选择的数组的任何位置检索我的对象,然后能够在该特定索引处从该对象中选择属性变量.
//PsuedoCode
array {
position 0: NoteData object {
attackTime = …Run Code Online (Sandbox Code Playgroud) 我加载我的数组NSDocumentDirectory:
self.images = [NSMutableArray new];
for(int i = 0; i <= 100; i++)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]];
NSLog(@"savedImagePath=%@",savedImagePath);
if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){
[images addObject:[UIImage imageWithContentsOfFile:savedImagePath]];
NSLog(@"file exists");
}
NSLog(@"file does not exist");
}
NSLog(@"Count : %d", [images count]);
Run Code Online (Sandbox Code Playgroud)
这是我实现我的数组的地方:
- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
view = [[UIImageView alloc] initWithImage:[images objectAtIndex:index]];
return view;
}
Run Code Online (Sandbox Code Playgroud)
我在NSInteger上面添加了上面的数组viewDidLoad:
for (int x=0; …Run Code Online (Sandbox Code Playgroud) 在我的项目中,我正在存储NSMutableArray.NSMutableDictionary我正在FOR循环执行此任务.实际情况是,
在for循环中
首先,我存储了一些对象NSMutableArray.
2.Stored这NSMutableArray在NSMutableDictionary与关键.
3. NSMutableArray然后在迭代之前删除所有对象.
我的问题,如果我在NSLog NSMutableArray和NSMutableDictionay第2步之后,我可以看到数组和字典中的对象.但是在第3步之后,如果我是NSLog NSMutableDictionay,只显示键,字典中的对象也被删除了.我错了.请建议一些解决方案.
这是我在项目中使用的代码.
for (int j=0; j<wordsofline.count; j++) {
NSString *importstr=[wordsofline objectAtIndex:j];
NSString *importtrim=[importstr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([importtrim rangeOfString:@"FROM"].location != NSNotFound) {
NSArray *splitImportMIB = [importtrim componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"array %@ ,,,%@",splitImportMIB,muArrImport);
[muDictImport setObject:muArrImport forKey:[splitImportMIB objectAtIndex:1]];
NSLog(@"Dictionary %@",muDictImport);
[muArrImport removeAllObjects];
NSLog(@"Dictionary %@",muDictImport);
break;
}
if (![importtrim isEqualToString:@""] && [importtrim rangeOfString:@"FROM"].location == NSNotFound) {
[muArrImport addObject:importtrim];
}
} …Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的数据库,并放入了NSUserDefaults。我的数据库是NSMutableArray,其中包含字典和数组。从NSUSerDefaults创建NSMutableArray时,无法将任何对象添加到NSMutableArray内的可变对象中。这是我的代码:
NSMutableArray *arrayOne = [NSMutableArray arrayWithContentsOfFile:[self createEditableCopyOfIfNeededWithFileName:@"Form.plist"]];
NSUserDefaults *ayarlar = [NSUserDefaults standardUserDefaults];
[ayarlar setObject:arrayOne forKey:@"form"];
NSMutableArray *arrayTwo = [NSMutableArray arrayWithArray:[[ayarlar objectForKey:@"form"] mutableCopy]];
[[[arrayTwo objectAtIndex:0] objectForKey:@"itemlar"] addObject:@"hop"];
Run Code Online (Sandbox Code Playgroud)
这是错误:
“ NSInternalInconsistencyException”,原因:“-[__ NSCFArray insertObject:atIndex:]:发送给不可变对象的变异方法”
我该如何进行这项工作?谢谢大家。
nsmutablearray ×10
ios ×5
objective-c ×4
iphone ×3
cocoa-touch ×2
c++ ×1
cocoa ×1
database ×1
ios4 ×1
nsarray ×1
nslog ×1
uiswitch ×1
uiwebview ×1
xcode ×1
xcode4.2 ×1