如何在objective-c中将NSMutableArray转换为NSArray ?
我正在开发iphone应用程序.
我使用NSCoder.
MyApplication.h
#define ITEMS_KEY @"items"
#define CATEGORIES_KEY @"categories"
#import <UIKit/UIKit.h>
@interface MyApplicationData : NSObject <NSCoding, NSCopying> {
NSMutableArray* items;
NSMutableArray* categories;
}
@property (nonatomic ,retain) NSMutableArray* items;
@property (nonatomic, retain) NSMutableArray* categories;
@end
Run Code Online (Sandbox Code Playgroud)
Myapplication.m
#import "MyApplicationData.h"
@implementation MyApplicationData
@synthesize items;
@synthesize categories;
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:items forKey:ITEMS_KEY];
[aCoder encodeObject:categories forKey:CATEGORIES_KEY];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init]){
self.items = [aDecoder decodeObjectForKey:ITEMS_KEY];
self.categories = [aDecoder decodeObjectForKey:CATEGORIES_KEY];
}
return self;
}
#pragma mark -
#pragma mark NSCopying …Run Code Online (Sandbox Code Playgroud) 如何修剪NSMutableString中的""和"\n"?
我想在选择表格单元格时显示Web视图
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[uiWebView loadRequest:[NSURL URLWithString:@"http://www.google.com"]];
[self.navigationController pushNavigationItem:uiWebView animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
日志
-[UINavigationController pushNavigationItem:animated:]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud) appdata.items是NSMutableArray.
我不能编译这段代码.错误代码是"prop.173具有不完整类型".
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];
}
Run Code Online (Sandbox Code Playgroud) Objective-C的
Conflict types for '-(NSInteger)amountYear:(NSInteger)searchYear Month:(NSInteger)searchMonth'
Run Code Online (Sandbox Code Playgroud)
我怎么能摆脱这个警告?
.M
#import "MyAppDataController.h"
#import "MyApplicationData.h"
@implementation MyAppDataController
@synthesize appdata;
static id _instance = nil;
+ (id)instance
{
@synchronized(self) {
if (!_instance) {
_instance = [[MyAppDataController alloc] init];
}
}
return _instance;
}
+ (id)allocWithZone:(NSZone*)zone
{
@synchronized(self) {
if (!_instance) {
_instance = [super allocWithZone:zone];
return _instance;
}
}
return nil;
}
- (id)copyWithZone:(NSZone*)zone
{
return self;
}
- (id)retain
{
return self;
}
- (unsigned)retainCount
{
return UINT_MAX;
}
- (void)release
{
} …Run Code Online (Sandbox Code Playgroud) 此功能执行得不好.
-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
NSLog(@"A");
NSArray* ary = [[NSArray alloc] initWithObjects:a, b, c, nil];
NSLog([ary description]);
NSLog(@"B");
}
Run Code Online (Sandbox Code Playgroud)
日志
[Session started at 2009-11-07 20:46:10 +0900.]
2009-11-07 20:46:19.170 xxx[2374:207] A
Run Code Online (Sandbox Code Playgroud)
原因是什么?
编辑:
我试过了.但它没有执行.
-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
NSLog(@"A");
NSArray* ary = [[NSArray alloc] initWithObjects:a, b, c, nil];
NSLog(@"%@", [ary description]);
NSLog(@"B");
}
Run Code Online (Sandbox Code Playgroud)
日志
[Session started at 2009-11-07 21:25:37 +0900.]
2009-11-07 21:25:48.738 xxx[2455:207] A
Run Code Online (Sandbox Code Playgroud)