ios - 清除 NSMutableArray 以存储新的 json 它

标签 ios objective-c arrays json nsmutablearray

我在网络请求中获取了一个 json 数组并将其存储在 NSMutableArray 中。第一次运行效果很好。但是当我刷新我的屏幕,并用它做一个新的网络请求将 json 存储在同一个数组中时,问题就开始了

我如何清除数组以在其中存储来自网络请求的新 json?

在我存储 json 的数组下面:

单例类.h

#import <Foundation/Foundation.h>
@class Singleton;
@protocol SingletonDelegate <NSObject>
@end

@interface SingletonClass : NSObject

// ...
@property (nonatomic, strong) NSMutableArray *uData;

@end

单例类.m

#import "SingletonClass.h"

@implementation SingletonClass

static SingletonClass *sharedInstance = nil;

// Get the shared instance and create it if necessary.
+ (SingletonClass *)sharedInstance {
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }

    return sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        // ...
        self.uData = [[NSMutableArray alloc] init]; // store json array from web-request
    }
    return self;
}
@end

这里有 3 个变体,我尝试将 json 重新存储在数组中。第一个网络请求按预期工作,但如果我执行另一个网络请求,我会收到错误...

网络接口(interface).h

#import "AFHTTPRequestOperationManager.h"
#import "SingletonClass.h"


@interface WebApi : AFHTTPRequestOperationManager <SingletonDelegate>

@property (nonatomic, strong) SingletonClass *sshare;
-(void)getUserStream;

@end

WebApi.m - 包括 3 个变体

#import "WebApi.h"

#define kApiHost @"http://foo.net"
#define kApiPath @"bar"

@implementation WebApi

-(id)init {
    self = [super init];
    if (self != nil) {
        self.sshare = [SingletonClass sharedInstance];
    }
    return  self;
}

-(void)getUserStream {

    NSString *URLString = [NSString stringWithFormat:@"%@/%@/pics/user", kApiHost, kApiPath];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [self setAuthHeader:manager];

    // clear uData
    [self.sshare.uData removeAllObjects];  // Breakpoint set

    [manager GET:URLString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        // Variant 1
        self.sshare.uData = responseObject;

        // Variant 2
        NSMutableArray *tmp = [NSMutableArray arrayWithArray:responseObject];
        self.sshare.uData = [tmp copy];

        // Variant 3
        NSMutableArray *tmp = [NSMutableArray arrayWithArray:responseObject];
        self.sshare.uData = [tmp mutableCopy];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"Error: %@, responseString: %@", error, operation.responseString);
    }];
}

@end

变体 1 错误,在断点 removeAllObjects 之后

[1417:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
*** First throw call stack:
(0x2f851f03 0x39fe6ce7 0x2f851e45 0x2f7cee7b 0x2f79b769 0x3d377 0x3aeb5 0x320b66c7 0x320b6663 0x320b6633 0x320a1d7b 0x321e1eef 0x3a4cfd53 0x3a4d4817 0x3a4cfd3f 0x3a4d13ef 0x3a4d2673 0x2f81c679 0x2f81af45 0x2f7857a9 0x2f78558b 0x346f26d3 0x320e4891 0x63569 0x3a4e4ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

变体 2 错误,在 removeAllObjects 断点之后

[1425:60b] -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x156f5910
[1425:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x156f5910'
*** First throw call stack:
(0x2f851f03 0x39fe6ce7 0x2f855837 0x2f85412f 0x2f7a30d8 0x2c2ff 0x29e3d 0x320b66c7 0x320b6663 0x320b6633 0x320a1d7b 0x321e1eef 0x3a4cfd53 0x3a4d4817 0x3a4cfd3f 0x3a4d13ef 0x3a4d2673 0x2f81c679 0x2f81af45 0x2f7857a9 0x2f78558b 0x346f26d3 0x320e4891 0x52569 0x3a4e4ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

变体 3 错误,在 removeAllObjects 断点之后

[1392:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds for empty array'
*** First throw call stack:
(0x2f851f03 0x39fe6ce7 0x2f7880cd 0xefbef 0x321b4199 0x3215b3fb 0x3215ac51 0x32081305 0x31cfd31b 0x31cf8b3f 0x31cf89d1 0x31cf83e5 0x31cf81f7 0x31d4bd45 0x34b1b75d 0x3053c5c9 0x2f811c4d 0x2f81c83f 0x2f81c7db 0x2f81afa7 0x2f7857a9 0x2f78558b 0x346f26d3 0x320e4891 0x119569 0x3a4e4ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

您的第一个变体是因为您在某处获得了 NSArray,而不是 NSMutableArray(可能是在您第一次获得 JSON 时)。您可能想使用 mutableCopy 而不是 arrayWithArray。

但这一切都没有实际意义:您无需为此大惊小怪。当您将第一个 JSON 转换为数组时,将其保留为数组(除非您需要更改其内容)。然后当你得到你的第二个 json 数组时,只需分配你的数组指针指向它,而不是原来的。像这样:

@property(nonatomic, strong) NSArray * myCurrentJson;
self.myCurrentJson = [mydownloadedJSonString JSONvalue];

然后在您的代码中稍后获得一个新的 JSON 数组时,只需设置:

self.myCurrentJson = [myNewDownloadedJsonString JSONvalue];

这样 myCurrentJson 总是指向一个有效的 json block ,你不必为可变的而烦恼,因为那些数组不会改变。当您替换数组时,旧数组不再有任何引用,因此 ARC 将在内存中释放它。如果您需要改变值,只需对原始数组执行 mutableCopy,然后在该数组中进行更改。

关于ios - 清除 NSMutableArray 以存储新的 json 它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23255049/

相关文章:

ios - 向 sceneKit View 添加了按钮,但它有延迟

javascript - UIWebview 中的自动凭据输入 - iOS

objective-c - 滚动时图像加载到错误的 UITableViewCell 上

ios - 如何在 iOS 上获取 "internet time"?

IOS 检查手机语言的最佳和安全方法是什么?

arrays - MethodError: 没有方法匹配 Weights(::Array{Any,1},::Float64)

C:应用于数组与指针的&符号运算符

ios - 检测 iOS 中的慢速网络

ios - UICollectionView 将单元​​格存储在数组中以便快速加载

python - 找到最小和最大像素的值