标签: nsarray

枚举包含字典的数组会产生意外的输出

我的问题是为什么它输出日志中的最后4行(见下文)......这些对象是日志中早先打印的字典的一部分,不应该位于数组的末尾?我在这里缺少一些基本的东西...... thx

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSURL URLWithString: @"www.stanford.edu"], 
    @"Stanford University", 
    [NSURL URLWithString: @"www.apple.com"], 
    @"Apple shop", 
    [NSURL URLWithString: @"cs193p.stanford.edu"], 
    @"CS193P course", 
    [NSURL URLWithString: @"itunes.stanford.edu"], 
    @"Stanford on iTunes U", 
    [NSURL URLWithString: @"stanfordshop.com"], 
    @"Stanford Mall", 
    nil];

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:
    [NSString init],
    [NSURL URLWithString: @"www.stanford.edu"],
    [NSProcessInfo processInfo],
    dictionary,
    [@"Mutable string example" mutableCopy],
    [@"another mutable string" mutableCopy]];

NSEnumerator *enumerator = [myArray objectEnumerator];
id object;

while ((object = [enumerator nextObject])) {
    NSLog([object description]);
}
Run Code Online (Sandbox Code Playgroud)

2009-07-02 09:35:12.756 WhatATool [6407:10b] NSString
2009-07-02 09:35:12.756 WhatATool …

enumeration objective-c nsdictionary nsarray

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

你可以从文件夹内容构建一个项目数组吗?

假设我的项目中有一个文件夹,其中包含许多png文件(或mp3文件或mov文件等).

我想创建一个NSArray,其中填充了该文件夹中的项目...例如,图像文件夹的UIImages数组(或者只是图像名称的NSStrings数组).

这可能吗?我知道我可以创建一个包含所有名称的plist,或者在代码中单独命名项目,但这会打破DRY(不要重复自己)规则.有没有人解决这个问题?

iphone dry objective-c plist nsarray

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

我的CoreData获取请求实际返回的是什么?

我从数据存储中取出一些对象,但结果并不是我所期待的.我是CoreData的新手,但我相当肯定这应该有用.我错过了什么?

请注意,User是一个有效的托管对象,我将其头文件包含在此代码中,并且该UserID是该类的有效属性.

NSFetchRequest *requestLocal = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:messageManagedObjectContext];
[requestLocal setEntity:entity];
// Set the predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY UserID IN %@", userList];
[requestLocal setPredicate:predicate];
// Set the sorting
... sorting details removed but exist and are fine ...
// Request the data
NSArray *fetchResults = [messageManagedObjectContext executeFetchRequest:requestLocal error:&error];
[requestLocal release];

for (int i; i < [fetchResults count]; i++) {
    [fetchResults objectAtIndex:i].UserID = ...<----HERE
}
Run Code Online (Sandbox Code Playgroud)

fetchResults不是User对象的数组吗?不会[fetchResults objectAtIndex:i]成为User对象吗?为什么在构建" 非结构或联合的成员'UserID'请求时"会出错?

对不起,如果这是一个基本错误,我显然缺少一些基本概念.我做了大量的搜索,看起来应该是正确的.(我也试过快速枚举,但它抱怨fetchResults项目不是有效的Objective C对象,我认为实际上是同样的错误.)


更新: …

iphone core-data objective-c nsarray

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

循环遍历NSArray objectAtIndex

NSInteger*count = [monLessonArrayA count]; for(i = 0; i <count; i ++){arrayVar = [monLessonArrayA objectAtIndex:i]; }

我得到一个错误,说我是未申报的,我怎么能将objectAtIndex设置为i所以我可以循环增加它每次?

谢谢.

iphone object objective-c nsarray

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

当NSArray元素被释放时会发生什么?

假设我创建了一些对象并将它们添加到数组中.

House *myCrib = [House house];
House *johnHome = [House house];
House *lisaHome = [House house];
House *whiteHouse = [House house];

NSArray *houses = [NSArray arrayWithObjects: myCrib, johnHome, lisaHome, whiteHouse, nil];
Run Code Online (Sandbox Code Playgroud)

通常,所有House对象的保留计数为2,但它们将被自动释放一次.过了一会儿,我决定释放myCrib,即使我不是主人 - 我从未保留或初始化.

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

保留计数应该降为零,我的对象应该被释放.我现在的问题是:这种非法行为会导致我的应用程序错误地工作甚至崩溃,或者NSArray只是将我的对象从列表中删除而后果不好.

我正在寻找一种维护对象列表的方法,但我希望列表能够自我维护.当某个对象消失时,我希望它的引用能够优雅地自动地从我的数组中消失.我正在考虑子类化或包装NSArray.

谢谢.

null object objective-c foundation nsarray

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

将nil传递给arrayWithArray的结果是什么:?

当你通过会发生什么nil来arrayWithArray:?

假设我有以下代码:

NSMutableArray *myArray = [NSMutableArray arrayWithArray:someOtherArray];

如果someOtherArray碰巧是nil,将myArray是nil或将是一个空的可变数组?

cocoa objective-c nsmutablearray nsarray

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

使用nspredicate过滤带字典的数组失败

我正在尝试使用谓词从NSMutableArray中获取NSDictionary:

NSMutableArray *arr = [NSMutableArray arrayWithArray:[self createArrayFromCSV:savedCSV withDelimiter:delim firstLineContainsKeys:YES]];

// The method «createArrayFromCSV returns an array of dictionaries

NSLog(@"Array %@", [arr objectAtIndex:0]);  // works fine

// spits out: 

// Array {
//     product = "MyProduct";
//     "id_product" = 29;
// }

// idField and idVal are passed to the method

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ == %@)", idField, idVal];

NSLog(@"predicate %@", predicate);

// spits out predicate "id_product" == "29"

NSArray *filtered = [arr filteredArrayUsingPredicate:predicate];

NSLog(@"Filtered %@", filtered);

// Filtered is …
Run Code Online (Sandbox Code Playgroud)

cocoa nsdictionary nsarray nspredicate

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

为什么我不能比较新阵列的数量?

谁可以给我解释一下这个 ?

if(0 <= -1)
    NSLog(@"Thank god");
if([NSArray new].count <= -1)
    NSLog(@"What the **** ? %i", [NSArray new].count);
if([[NSArray alloc] init].count <= -1)
    NSLog(@"What the **** ? %i", [[NSArray alloc] init].count );
Run Code Online (Sandbox Code Playgroud)

输出是两次What the **** ? 0,我期待没有输出我期望有0作为计数.

如果我将计数放入int或记录它,则输出0(零),但if语句在此处生成true.

if-statement objective-c nsarray ios

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

从NSString中删除重复的单词并另存为新的NSString

我有一些场景,我可能有一个包含几个单词的NSString,其中一些是重复的.我想要做的是采取一个看起来像这样的字符串:

One Two Three Three Three Two Two Two One One Two Three
Run Code Online (Sandbox Code Playgroud)

并使它看起来像:

One Two Three
Run Code Online (Sandbox Code Playgroud)

可能有时候原始NSString的确切长度也不同.到目前为止我所拥有的是:

NSString *hereitis = @"First Second Third Second Third First First First";
    NSArray *words = [hereitis componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSCountedSet *countedSet = [NSCountedSet setWithArray:words];
    NSMutableArray *finalArray = [NSMutableArray arrayWithCapacity:[words count]];

for(id obj in countedSet) {
    if([countedSet countForObject:obj] == 1) {
        [finalArray addObject:obj];
    }
}
NSString *string = [finalArray componentsJoinedByString:@" "];
NSLog(@"String%@", string);
Run Code Online (Sandbox Code Playgroud)

但是,这只是在我的数组中返回String,而不是任何单词.

objective-c nsstring nsmutablearray nsarray ios

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

Swift:NSArray要设置?

我正在尝试将其转换NSArray为Swift Set.

没有太多运气.

这样做的正确方法是什么?

例如,如果我有一个NSArray数字:

@[1,2,3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)

如何Set从中创建Swift NSArray?

set nsarray swift

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