views:

1258

answers:

2

appdata.items is NSMutableArray.

I connot compile This code. Error code is "prop.173 has an incomplete type".

NSInteger compareInfo(id aInfo1, id aInfo2, void *context){
  NSDate* info1 = [aInfo1 objectAtIndex:2];
  NSDate* info2 = [aInfo2 objectAtIndex:2];
  return [info1 compare:info2];
}

-(void)saveData{
  NSData* data = [[NSMutableData alloc] init];
  appdata.items = [appdata.items sortUsingFunction:compareInfo context:NULL];
  NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  [archiver encodeObject:appdata forKey:DATAKEY];
  [archiver finishEncoding];
  [data writeToFile:[self dataFilePath] atomically:YES];
  [archiver release];
  [data release];
}
+1  A: 

This link: http://www.cocoadev.com/index.pl?SortUsingSelector has some examples of a working sortUsingFunction. (scroll past the sortUsingSelector stuff).

Kenny Winker
+3  A: 

The method sortUsingFunction:context: is for NSMutableArray, not NSArray, which sorts the contents of the mutable array. The method you want is sortedArrayUsingFunction:context: which will return a sorted array which you can assign, as you're currently trying to do.

Unless, of course, items is an NSMutableArray, in which case you can call sortUsingFunction:context: but as it doesn't return anything, so you don't assign it to items.

Daniel Tull