我正在尝试读取txt文件并将其存储在NSArray中.这是我的代码,但似乎缺少一些我不知道的东西!
NSURL *url=[NSURL URLWithString:@"http://www.google.com/robots.txt"];
NSMutableArray *robots=[NSMutableArray arrayWithContentsOfURL:url];
NSLog(@"%@",robots);
Run Code Online (Sandbox Code Playgroud) 我正在尝试将以下plist加载到NSArray中:

这是我用来加载数据的代码:
// Load the data from the plist file
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"MyData" ofType:@"plist"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:filepath];
Run Code Online (Sandbox Code Playgroud)
但是,当我在加载数据后放置一个断点时,我注意到该数组是空的.请有人帮我理解出了什么问题?
我试图找到以特定字符串开头的NSStrings数组中的所有对象,并返回包含所有这些对象的较小数组.例如,您可以拥有包含对象的数组:
'Cat',
'Dog',
'Dolphin',
'Whale'
Run Code Online (Sandbox Code Playgroud)
当它搜索的字符串是do一个新的NSArray时,只返回Dog和Dolphin对象.
我正在尝试获取一组键,其中a中相应键的NSDictionary值为@ YES.
对于EG,:Starting Dictionary:
NSDictionary* dict = @{@"key1" : @YES, @"key2": @YES, @"key3": @NO, @"key4": @NO};
Run Code Online (Sandbox Code Playgroud)
期望的结果:
NSArray* array = @[@"key1", @"key2"];
Run Code Online (Sandbox Code Playgroud)
我尝试用NSPredicates和subpredicates做这个,但找不到一个做我想要的.我的"工作"但丑陋的解决方案是:
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:0];
NSDictionary* dict = @{@"key1" : @YES, @"key2": @YES, @"key3": @NO, @"key4": @NO};
for (NSString* key in [dict allKeys]) {
if ([[dict objectForKey:key] isEqualToNumber:@YES])
[array addObject:key];
}
Run Code Online (Sandbox Code Playgroud)
什么是更好的方法,可能与NSPredicates?
好吧,我遇到一个nsarray的问题,它是从nsdictionary添加重复的对象.
情况是这样的:我有一个带有一系列字典的plist文件.每个字典都有'codigo'和'var'.
我做的是我检查每个'var'值是否为<0,如果它是'codigo','var'将使用NSAttributedStrings获得颜色.
当我遍历数组并将评估的'var'添加到新数组中时,问题就来了.我在最终的数组中得到重复的对象.
编辑:解决方案源自xlc0212的答案和聊天帮助:
_timer = [NSTimer scheduledTimerWithTimeInterval:0.020 target:self
selector:@selector(time:) userInfo:nil repeats:YES];
NSDictionary *redAttrs = @{NSForegroundColorAttributeName:[UIColor redColor]};
NSDictionary *greenAttrs = @{NSForegroundColorAttributeName:[UIColor colorWithRed:0.118 green:0.506 blue:0.000 alpha:1.000]};
NSDictionary *orangeAttrs = @{NSForegroundColorAttributeName:[UIColor orangeColor]};
NSString *stringUm = @"";
NSString *stringDois = @"";
NSString *stringTres = @"";
arrayAtivos = [[NSMutableOrderedSet alloc] init];
NSDictionary *item = [[NSDictionary alloc]init];
for (item in array){
NSString *alfa = [item objectForKey:@"var"];
float fltNumber = [alfa floatValue];
if (fltNumber < 0) {
stringUm = [NSString stringWithFormat:@"%@ %.2f ",[item objectForKey:@"codigo"], fltNumber]; …Run Code Online (Sandbox Code Playgroud) 我希望得到我的NSArray元素的平均值,并按以下方式将它们存储在一个新数组中.
NSArray* number;
NSMutableArray* Average;
[Average objectAtIndex:0]=[number objectAtIndex:0];
[Average objectAtIndex:1]=([number objectAtIndex:0] + [number objectAtIndex:1])/2;
[Average objectAtIndex:2]=([number objectAtIndex:0] + [number objectAtIndex:1] + [number objectAtIndex:2])/3;
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用2 for循环
for (int i =0; i<[number count]; i++){
for (int j=0; j<i; j++){
temp+=[number objectAtIndex:j];
}
[Average addObject: temp/(i+1)];
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以用更有效的方式帮助我
谢谢
我正在开发一个IOS应用程序.如何从NSArray列表中获取对象.避免出现内存错误的最佳方法是什么?你能告诉我正确的方法吗?谢谢
Person *person = [[[Person alloc] init] autorelease];
person = [self.userFavourites objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
Person *person = [self.userFavourites objectAtIndex:0];
[person retain];
//Make the required action
[person release];
Run Code Online (Sandbox Code Playgroud)
我的应用程序崩溃在不同的地方,但错误是相同的,只有索引有时更改,任何想法为什么发生这种情况我也附加了我的截图:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:
(0x20d4012 0x1e34e7e 0x20760b4 0x152ba 0xdc4067 0xdc42db 0x14fca 0x16aa7 0x239253f 0x23a4014 0x2393fd6 0x23a4014 0x239b8b7 0x2397405 0x2394768 0x207aaf5 0x2079f44 0x2079e1b 0x2ca87e3 0x2ca8668 0xd78ffc 0x2b8d 0x2ab5)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Run Code Online (Sandbox Code Playgroud)
我已经尝试调试它以确定导致崩溃的确切位置或代码行但没有成功.它只是随机崩溃
谢谢
编辑1:
好吧,这是代码:
一旦获取,我将它们保存为:
-(void)saveTweetsArray:(NSMutableArray *)array{
NSString *dbPath = [DatabaseHelper databasePath];
sqlite3_stmt *statement;
if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) {
NSString *insertSql = [NSString stringWithFormat:@"delete …Run Code Online (Sandbox Code Playgroud) 以下是我的代码.当循环执行NSArray的NSDictionary对象,并试图将追加到一个数组我保持的NSLog,显示了正确的结果,但结果NSArray只包含的最后一个记录.
for (NSDictionary *entry in entries) {
NSString* projID = [entry objectForKey:@"NM_PROJECT"];
NSArray *projectNames = [[NSArray alloc] initWithObjects:projID,nil];
_projectpicker.delegate = self;
_projectpicker.dataSource = self;
NSLog(@"Error : %@", projID);
}
Run Code Online (Sandbox Code Playgroud)
日志结果:Test1 test2 test3 test4
当附加到NSArray(Projectnames)时,我收到了最后的数据..
我今天在iOS中发现了一些奇怪的东西,我解决了它,但我想问一下是否有更好的(更多iOS)解决方案.
我想要做的是:
循环遍历字典数组并为iOS中的每个字典添加新密钥
这是代码(这不是我的原始代码,它更复杂,但我完成了这个代码以说明容易点):
- (void)testLoopThruArrayOfDictionaryAndAddNewKeyToEachDictionary
{
NSMutableArray* a = [[NSMutableArray alloc] init];
[a addObjectsFromArray:@[ @{@"one": @11, @"two": @12, } ]];
[a addObjectsFromArray:@[ @{@"one": @21, @"two": @22, } ]];
NSLog(@"%@", a);
/*
// NOT WORKING
for (NSMutableDictionary* dict in a)
{
// -[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x8b5bca0
[dict setObject:@3 forKey:@"three"];
}
*/
// THIS IS WORKING
for (int i=0; i<[a count]; i++)
{
// make tmp copy
NSMutableDictionary *tmpA = [a[i] mutableCopy];
[tmpA setObject:@3 forKey:@"three"];
// put …Run Code Online (Sandbox Code Playgroud) objective-c nsdictionary nsmutablearray nsarray nsmutabledictionary
nsarray ×10
objective-c ×8
ios ×4
nsdictionary ×2
average ×1
debugging ×1
duplicates ×1
filter ×1
ios6 ×1
iphone ×1
nspredicate ×1
nsstring ×1
plist ×1
robots.txt ×1
search ×1