【问题标题】:Trouble with saving an NSMutableArray with url's at NSUserDefaults在 NSUserDefaults 保存带有 url 的 NSMutableArray 的问题
【发布时间】:2014-02-18 05:36:43
【问题描述】:

我创建了一个应用程序,该应用程序基于我阅读的内容并录制音频,然后尝试通过其唯一 url 路径将所有音频文件永久保存到 mutablearray 并从那里保存到 NSUserDefaults 但我正在尝试设置非属性列表对象错误。这是我的代码崩溃的部分:

-(void)audioRecorderDidFinishRecording: (AVAudioRecorder *)recorder successfully:(BOOL)flag{
if(flag==true){

    [self.recordings addObject:_audioRecorder.url];
     NSLog(@"prwti einai edw %@", self.recordings);

    [self.userDefaults setObject:self.recordings forKey:FTS];
    [self.userDefaults synchronize];
    NSLog(@"%@", self.recordings);
}}

有什么建议吗?请现在我是 iOS 编程的新手并试图解决问题。谢谢。

- (void)viewDidLoad{
[super viewDidLoad];

// get the NSUserDefaults object for the app
self.userDefaults = [NSUserDefaults standardUserDefaults];

// get NSDictionary of the app's tag-query pairs
NSArray *dictionary = [self.userDefaults arrayForKey:FTS];

// if dictionary is nil, create empty NSMutableDictionary;
// otherwise, create NSMutableCopy of dictionary
if (dictionary == nil)
    self.recordings = [[NSMutableArray alloc] init];
else
    self.recordings = [dictionary mutableCopy];


//na allaxoume to onoma tou arxeio se monadiko gia to kathena
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"ddMMMYY_hhmmssa";
NSString *date=[[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".caf"];

_playButton.enabled = NO;
_stopButton.enabled = NO;

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:date];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16], AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                nil];
NSError *error = nil;

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

_audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
                                             settings:recordSettings
                                                error:&error];
_audioRecorder.delegate=self;
if (error) {
    NSLog(@"error: %@", [error localizedDescription]); }

else {
    [_audioRecorder prepareToRecord];
}}

【问题讨论】:

    标签: avfoundation nsuserdefaults avaudiorecorder avaudiosession


    【解决方案1】:

    试试你的网址,比如 NSData

    [self.recordings addObject:_audioRecorder.url];
    
    NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:self.recordings.count];
    for (NSString *audioPath in self.recordings) { 
        NSData *pathAudioData = [NSKeyedArchiver archivedDataWithRootObject:audioPath];
        [archiveArray addObject:pathAudioData];
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:archiveArray forKey:FTP];
    

    请记住,您只能将 NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate 之类的内容存储在 NSUserDefaults 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 2013-04-16
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多