标签: nsarray

如何读取txt文件并存储在NSArray中?

我正在尝试读取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)

robots.txt objective-c nsarray

1
推荐指数
1
解决办法
2061
查看次数

无法将plist加载到NSArray中

我正在尝试将以下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)

但是,当我在加载数据后放置一个断点时,我注意到该数组是空的.请有人帮我理解出了什么问题?

objective-c plist nsarray

1
推荐指数
1
解决办法
1218
查看次数

NSStrings的NSArray,数组包含以string开头的对象

我试图找到以特定字符串开头的NSStrings数组中的所有对象,并返回包含所有这些对象的较小数组.例如,您可以拥有包含对象的数组:

'Cat',
'Dog',
'Dolphin',
'Whale'
Run Code Online (Sandbox Code Playgroud)

当它搜索的字符串是do一个新的NSArray时,只返回DogDolphin对象.

search filter nsstring nsarray ios

1
推荐指数
1
解决办法
7167
查看次数

从NSDictionary获取包含NSNumber bools的键列表,其值为@YES

我正在尝试获取一组键,其中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

objective-c nsdictionary nsarray nspredicate ios6

1
推荐指数
1
解决办法
4781
查看次数

如何避免在NSArray中插入重复的对象?

好吧,我遇到一个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)

objective-c duplicates nsarray

1
推荐指数
1
解决办法
6683
查看次数

新NSArray中NSArray和商店值的平均值

我希望得到我的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)

任何人都可以用更有效的方式帮助我

谢谢

average objective-c nsmutablearray nsarray

1
推荐指数
1
解决办法
3940
查看次数

IOS从NSArray获取对象

我正在开发一个IOS应用程序.如何从NSArray列表中获取对象.避免出现内存错误的最佳方法是什么?你能告诉我正确的方法吗?谢谢

建议1

Person *person = [[[Person alloc] init] autorelease];
        person = [self.userFavourites objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

建议2

Person *person = [self.userFavourites objectAtIndex:0];
[person retain];

//Make the required action

[person release];
Run Code Online (Sandbox Code Playgroud)

建议3

memory-management objective-c nsarray ios

1
推荐指数
1
解决办法
5640
查看次数

应用程序崩溃:索引在随机位置超出范围

我的应用程序崩溃在不同的地方,但错误是相同的,只有索引有时更改,任何想法为什么发生这种情况我也附加了我的截图:

*** 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)

iphone debugging nsarray ios

1
推荐指数
1
解决办法
1992
查看次数

将NSString附加到NSArray时只获取一个值

以下是我的代码.当循环执行NSArrayNSDictionary对象,并试图将追加到一个数组我保持的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)时,我收到了最后的数据..

objective-c nsarray ios

1
推荐指数
1
解决办法
219
查看次数

循环遍历字典数组并为iOS中的每个字典添加新密钥

我今天在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

1
推荐指数
1
解决办法
1437
查看次数