This question already has answers here:
Obj-C easy method to convert from NSObject with properties to NSDictionary?

(6个答案)


4年前关闭。




如何将NSObject转换为NSDictionary?

第一步,我已经将NSDictionary转换为NSObject,
QRCodeData *obj = [[QRCodeData alloc] initWithQRcodeData:myDictonary];

QRCodeData.h
@interface QRCodeData : NSObject
-(instancetype)initWithQRcodeData:(NSDictionary*)dictionary;
@end
QRCodeData.m
@implementation QRCodeData

-(instancetype)initWithQRcodeData:(NSDictionary*)dictionary
{
    self = [super init];
    if(self){

        self.name = dictionary[@"userName"];
        self.phoneNumber = dictionary[@"mobileNo"];

    }
    return self;

}
@end

我想从对象中获取我的字典,有可能得到吗?

请先帮助并表示感谢。.

最佳答案

您可以简单地获取字典,例如

  NSDictionary *dict = @{@"userName": obj.name ,@"mobileNo" : obj.phoneNumber };

这里objQRCodeData的对象。

希望这会有所帮助:)

关于ios - 将NSObject转换为NSDictionary ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37292889/

10-13 04:23