我正在尝试使用键@“ Channel”以升序对NSMutableArray(带有字典对象)进行排序(有多个通道条目,为了便于阅读,我已将其缩短了):
我试图使用排序:
NSSortDescriptor *channelDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Channel" ascending:YES selector:@selector(compare:)];
NSArray *descriptors = @[channelDescriptor];
[self.tempArrayDuplicateDataForAppWatch sortUsingDescriptors:descriptors];
数据:
{
Airings = (
{
AiringTime = "2015-08-24T19:00:00Z";
AiringType = Unknown;
CC = 1;
Category = Movie;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 120;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
MovieRating = R;
ProgramId = 20306212;
SAP = 0;
Sports = 0;
Stereo = 1;
Subcategory = horror;
TVRating = "TV-MA";
Title = Chernobyl;
}
);
CallLetters = Syfy;
Channel = 151;
ChannelImages = (
{
AspectRatio = "109:79";
ImageCaption = "Source logo";
ImageCredit = Syfy;
ImageCreditDisplay = false;
ImageExpiryDateTime = "<null>";
ImageFormat = jpg;
ImageFormatId = 144;
ImageHorizontalResolution = 720;
ImageId = 74975320;
ImageOwner = Syfy;
ImageTitle = Syfy;
ImageType = "Station Logo";
ImageUrl = ;
ImageVerticalResolution = 521;
LastUpdate = "2014-07-11T22:05:13Z";
ObjectId = 1232;
ObjectName = "Syfy (East)";
ObjectType = Source;
ParentImageId = 16795667;
}
);
ChannelSchedules = (
);
DisplayName = Syfy;
IconAvailable = 0;
IsChannelOverride = 0;
Order = 1510647;
ParentNetworkId = 0;
ServiceId = 82368;
SourceAttributeTypes = "";
SourceAttributes = 0;
SourceId = 1232;
SourceLongName = "Syfy (East)";
SourceType = Basic;
Type = "24-Hours";
},
{
Airings = (
{
AiringTime = "2015-08-24T19:00:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 26463608;
SAP = 0;
SeriesId = 24828094;
Sports = 0;
Stereo = 1;
Subcategory = "game show";
TVRating = None;
Title = "Celebrity Game";
},
{
AiringTime = "2015-08-24T19:30:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 26332661;
SAP = 0;
SeriesId = 24828094;
Sports = 0;
Stereo = 1;
Subcategory = "game show";
TVRating = None;
Title = "Celebrity Game";
},
{
AiringTime = "2015-08-24T20:00:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 1055967;
SAP = 0;
Sports = 0;
Stereo = 0;
Subcategory = "game show";
TVRating = "TV-PG";
Title = "Family Feud";
},
{
AiringTime = "2015-08-24T20:30:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 1055967;
SAP = 0;
Sports = 0;
Stereo = 0;
Subcategory = "game show";
TVRating = "TV-PG";
Title = "Family Feud";
}
);
CallLetters = WDAF;
Channel = 4;
ChannelImages = (
{
AspectRatio = "23:9";
ImageCaption = "92x36;darklight";
ImageCreditDisplay = false;
ImageExpiryDateTime = "<null>";
ImageFormat = jpg;
ImageFormatId = 97;
ImageHorizontalResolution = 800;
ImageId = 4065300;
ImageTitle = FOX;
ImageType = "Station Logo";
ImageUrl = ;
ImageVerticalResolution = 313;
LastUpdate = "2015-06-22T16:50:52Z";
ObjectId = 134;
ObjectName = WDAF;
ObjectType = Source;
ParentImageId = 16814381;
}
);
ChannelSchedules = (
);
DisplayName = WDAF;
IconAvailable = 1;
IsChannelOverride = 0;
Order = 40002;
ParentNetworkId = 3;
ServiceId = 82368;
SourceAttributeTypes = "";
SourceAttributes = 0;
SourceId = 134;
SourceLongName = WDAF;
SourceType = Broadcast;
Type = "24-Hours";
}
我意识到@“ Channel”值是字符串格式,并且我不能使用“ compare:”我想知道是否有更好的方法。我非常感谢您的帮助!
再次感谢您的帮助和建议。我终于弄清楚了为什么NSSortDescriptor无法正常工作。@“ Channel”的值是一个字符串,因此排序无法正常进行。我必须一个一个地从NSMutableArray中提取每个词典,并将@“ Channel”的值更改为NSNumber / NSDecimalNumber,然后用新值替换旧的字符串值。然后,我用NSSortDescriptor进行了排序,并且它起作用了。
这是示例代码:
for (int i=0; i<[unsortedNSMutableArray count]; i++)
{
NSMutableDictionary *tempChannelInfo = [[NSMutableDictionary alloc] init];
tempChannelInfo = unsortedNSMutableArray[i];
NSString *tempChannelString = tempChannelInfo[@"Channel"];
NSNumber *convertedChannel = [NSDecimalNumber decimalNumberWithString:tempChannelString];
[tempChannelInfo setObject:convertedChannel forKey:@"Channel"];
[unsortedNSMutableArray replaceObjectAtIndex:i withObject:tempChannelInfo];
}
然后我排序:
NSSortDescriptor *channelDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Channel" ascending:YES selector:@selector(compare:)];
NSArray *descriptors = @[channelDescriptor];
[unsortedNSMutableArray sortUsingDescriptors:descriptors];
再次感谢您的所有帮助和建议!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句