我今天有一个关于Objective-C的问题涉及NSMutableArray.来自.net/c#背景我在处理这些事情时遇到了一些麻烦.
假设我有一个名为"Song"的对象
我的歌有3个属性:
我有一个NSMutableArray或NSArray,它拥有我所有的Song对象.
我将如何尝试"查询"我的阵列以获得仅具有(唯一)艺术家或流派的新阵列.
在.net中你会用DISTINCT子句编写一个简单的LINQ查询,如何在Objective-C中解决这个问题?我猜测谓词,但我很难找到解决方案.
提前致谢.
我需要删除所有对象NSMutableArray.我似乎无法通过枚举代码崩溃来做到这一点.
有人可以告诉我使用代码示例执行此操作的最佳方法吗?
是否可以直接将块存储在NSArray等集合中?
我需要删除添加的Null对象
[mutArrSkills addObject:[NSNull null]];
Run Code Online (Sandbox Code Playgroud)
我需要迭代吗?是否有任何函数可以从NSMutableArray中删除所有空值?
如果需要迭代,我该怎么做?
我有以下可变数组:
NSMutableArray *persons = [[NSMutableArray alloc]initWithObjects:person1, person2, person3, nil];
Run Code Online (Sandbox Code Playgroud)
其中每个人都是一个对象,其中包含(NSInteger)personAge和(NSString*)personName属性.现在我想通过personAge对这个数组进行排序.所以我尝试了以下方法:
[persons sortUsingComparator:
^NSComparisonResult(id obj1, id obj2)
{
Person *p1 = (Person*)obj1;
Person *p2 = (Person*)obj2;
return [p1.personAge compare: p2.personAge];
}];
NSLog(@"%ld", [persons componentsJoinedByString:@" "]);
Run Code Online (Sandbox Code Playgroud)
但我在返回行中收到"错误的接收器类型'NSInteger'(又名'long')"错误消息.另外我在NSLog行中有一个警告:"Format指定类型'long'但参数的类型为'NSString*'".我如何解决它?
我有一个Threaded应用程序,其中有一个NSMutableArray,包含NSManagedObjects,现在我希望我的数组可以被任何线程一次访问一次.那么我如何同步该数组,或者可以将锁定机制放在其上.
提前致谢 ...
我已经使用NSXML Parser在我的程序中使用NSMutableString值填充了NSMutableArray.
我可以使用objectatindex方法成功地从数组中获取结果,但是当我尝试在if()结构中组合该值时,它不起作用,但是NSLog显示它返回了正确的值.继承我日志窗口上的代码和输出:
int Total = 0;
int Adet = 0;
int LPGvolume = 0;
for(int i = 0;i<[yakitArray count];i++)
{
NSMutableString *yakitVal = [NSMutableString stringWithFormat:@"%@",[yakitArray objectAtIndex:i]];
NSLog(@"Yakitval: %@",yakitVal);
if(yakitVal != @"LPG")
{
NSMutableString *volumeVal = [volumeArray objectAtIndex:i];
Total = Total + [volumeVal integerValue];
}
else
{
NSLog(@"LPG Found!");
NSMutableString *volumeVal = [volumeArray objectAtIndex:i];
LPGvolume = [volumeVal integerValue];
}
NSMutableString *adetVal = [adetArray objectAtIndex:i];
Adet = Adet + [adetVal integerValue];
}
Run Code Online (Sandbox Code Playgroud)
并且控制台输出:
2011-01-10 16:58:10.885 iStationTouch3[39393:7907] Yakitval: Value1
2011-01-10 16:58:10.886 iStationTouch3[39393:7907] …Run Code Online (Sandbox Code Playgroud) 我来自JSON
[{ "照片":空}]
我使用这段代码
NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"photo"]];
Run Code Online (Sandbox Code Playgroud)
如果我想使用if(),我怎么检查它?
编辑
这是JSON数据
[{"photo":
[{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928459.jpg","title":"test picture","content":"this is description for test picture.\r\n\u8aac\u660e\u6587\u306a\u306e\u306b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb"}
,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928115.jpg","title":"nothing","content":"iMirai"}
,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1303276769.jpg","title":"iMirai","content":"Staff"}]}
]
Run Code Online (Sandbox Code Playgroud)
这是我的JSON解析器
NSError *theError = nil;
NSString *URL = [NSString stringWithFormat:@"http://www.yohyeh.com/apps/get_sub_detail.php?id=%@&menu=photo",g_id];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [string JSONValue];
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我使用类似于以下代码的基于块的枚举:
[[[rows objectForKey:self.company.coaTypeCode] objectForKey:statementType]
enumerateObjectsWithOptions:NSEnumerationConcurrent
usingBlock:^(id coaItem, NSUInteger idx, BOOL *stop) {
// block code here
}]
Run Code Online (Sandbox Code Playgroud)
我想在枚举过程中删除一些对象,具体取决于它们的对象值.
我怎么能这样做?我知道在枚举期间操纵可变数组或字典(NSMutableArray或NSMutableDictionary)通常是不可能的.
实现这个的最佳方法是什么?
谢谢!
objective-c nsmutablearray nsmutabledictionary ios objective-c-blocks
我有一个NSMutableArray包含NSIndexPath对象的东西,我想row按升序对它们进行排序.
什么是最短/最简单的方法?
这就是我尝试过的:
[self.selectedIndexPaths sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSIndexPath *indexPath1 = obj1;
NSIndexPath *indexPath2 = obj2;
return [@(indexPath1.section) compare:@(indexPath2.section)];
}];
Run Code Online (Sandbox Code Playgroud) nsmutablearray ×10
objective-c ×8
iphone ×3
ios ×2
cocoa ×1
compare ×1
ios4 ×1
json ×1
locking ×1
nsarray ×1
nsindexpath ×1
xcode ×1