Elysia Tools

在一个清晰的工作台里浏览工具、案例、可视化和主题指南。

热门分类

专题

语言
ZHESPTRUFRDEEN

Elysia Tools

导航

Objective-C

Objective-C: null 与空数组不同, 拒绝不同的根结构, 累积拆分的字符片段

选择代码卡片,追踪固定输入,区分本地计算与真实设备或服务器行为。

案例

这个案例集合中的条目

代码objectivec中等

JSON序列化

使用NSJSONSerialization将Objective-C对象转换为JSON字符串

难度

5/10

预计时间

25 min

标签

objectivec, macos, serialization, json

前置条件

Objective-C basics, Foundation framework

objectivec
// macOS Objective-C JSON Serialization Examples
// Using NSJSONSerialization for JSON encoding

#import <Foundation/Foundation.h>

// MARK: - 1. Serialize Dictionary to JSON

@interface JSONSerializer : NSObject

+ (NSData *)serializeDictionaryToJSON:(NSDictionary *)dictionary;
+ (NSString *)serializeDictionaryToJSONString:(NSDictionary *)dictionary;
+ (BOOL)writeDictionaryToJSONFile:(NSDictionary *)dictionary path:(NSString *)path;

@end

@implementation JSONSerializer

+ (NSData *)serializeDictionaryToJSON:(NSDictionary *)dictionary {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:0 // NSJSONWritingOptions
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Dictionary serialized to JSON data (%lu bytes)", (unsigned long)jsonData.length);
    return jsonData;
}

+ (NSString *)serializeDictionaryToJSONString:(NSDictionary *)dictionary {
    NSData *jsonData = [self serializeDictionaryToJSON:dictionary];

    if (!jsonData) {
        return nil;
    }

    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                  encoding:NSUTF8StringEncoding];

    NSLog(@"JSON String: %@", jsonString);
    return jsonString;
}

+ (BOOL)writeDictionaryToJSONFile:(NSDictionary *)dictionary path:(NSString *)path {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return NO;
    }

    BOOL success = [jsonData writeToFile:path
                                 options:NSDataWritingAtomic
                                   error:&error];

    if (error) {
        NSLog(@"File write error: %@", error.localizedDescription);
        return NO;
    }

    NSLog(@"JSON written to file: %@", path);
    return success;
}

@end

// MARK: - 2. Serialize Array to JSON

@interface JSONArraySerializer : NSObject

+ (NSData *)serializeArrayToJSON:(NSArray *)array;
+ (NSString *)serializeArrayToJSONString:(NSArray *)array;
+ (NSData *)serializeArrayToPrettyJSON:(NSArray *)array;

@end

@implementation JSONArraySerializer

+ (NSData *)serializeArrayToJSON:(NSArray *)array {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                       options:0
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Array serialized to JSON data");
    return jsonData;
}

+ (NSString *)serializeArrayToJSONString:(NSArray *)array {
    NSData *jsonData = [self serializeArrayToJSON:array];

    if (!jsonData) {
        return nil;
    }

    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                  encoding:NSUTF8StringEncoding];

    NSLog(@"JSON Array String: %@", jsonString);
    return jsonString;
}

+ (NSData *)serializeArrayToPrettyJSON:(NSArray *)array {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Array serialized to pretty JSON");
    return jsonData;
}

@end

// MARK: - 3. Serialize Custom Object to JSON

@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSArray *hobbies;

- (NSDictionary *)toDictionary;

@end

@implementation Person

- (NSDictionary *)toDictionary {
    return @{
        @"name": self.name ?: [NSNull null],
        @"email": self.email ?: [NSNull null],
        @"age": @(self.age),
        @"hobbies": self.hobbies ?: @[]
    };
}

@end

@interface CustomObjectSerializer : NSObject

+ (NSData *)serializePersonToJSON:(Person *)person;
+ (NSData *)serializePersonArrayToJSON:(NSArray *)people;

@end

@implementation CustomObjectSerializer

+ (NSData *)serializePersonToJSON:(Person *)person {
    NSDictionary *dict = [person toDictionary];

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Person serialized to JSON");
    return jsonData;
}

+ (NSData *)serializePersonArrayToJSON:(NSArray *)people {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:people.count];

    for (Person *person in people) {
        [array addObject:[person toDictionary]];
    }

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Person array serialized to JSON");
    return jsonData;
}

@end

// MARK: - 4. Serialize Nested Objects

@interface Company : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *location;
@property (nonatomic, strong) NSArray *employees;

- (NSDictionary *)toDictionary;

@end

@implementation Company

- (NSDictionary *)toDictionary {
    NSMutableArray *employeeArray = [NSMutableArray array];

    for (Person *employee in self.employees) {
        [employeeArray addObject:[employee toDictionary]];
    }

    return @{
        @"name": self.name,
        @"location": self.location,
        @"employees": employeeArray
    };
}

@end

@interface NestedObjectSerializer : NSObject

+ (NSData *)serializeCompanyToJSON:(Company *)company;

@end

@implementation NestedObjectSerializer

+ (NSData *)serializeCompanyToJSON:(Company *)company {
    NSDictionary *dict = [company toDictionary];

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Company serialized to JSON");
    return jsonData;
}

@end

// MARK: - Main Demonstration

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"=== macOS Objective-C JSON Serialization Examples ===\n");

        // 1. Serialize dictionary
        NSLog(@"--- 1. Serialize Dictionary ---");

        NSDictionary *userDict = @{
            @"id": @(1),
            @"username": @"john_doe",
            @"email": @"[email protected]",
            @"active": @(YES),
            @"balance": @(1234.56)
        };

        [JSONSerializer serializeDictionaryToJSONString:userDict];

        NSString *jsonPath = @"/tmp/user.json";
        [JSONSerializer writeDictionaryToJSONFile:userDict path:jsonPath];

        // 2. Serialize array
        NSLog(@"\n--- 2. Serialize Array ---");

        NSArray *fruits = @[@"Apple", @"Banana", @"Orange", @"Mango"];
        [JSONArraySerializer serializeArrayToJSONString:fruits];

        NSArray *numbers = @[@1, @2, @3, @4, @5];
        [JSONArraySerializer serializeArrayToJSONString:numbers];

        // 3. Serialize custom object
        NSLog(@"\n--- 3. Serialize Custom Object ---");

        Person *person = [[Person alloc] init];
        person.name = @"Alice Smith";
        person.email = @"[email protected]";
        person.age = 30;
        person.hobbies = @[@"Reading", @"Hiking", @"Photography"];

        NSData *personJSON = [CustomObjectSerializer serializePersonToJSON:person];
        if (personJSON) {
            NSString *jsonString = [[NSString alloc] initWithData:personJSON
                                                          encoding:NSUTF8StringEncoding];
            NSLog(@"Person JSON:\n%@", jsonString);
        }

        // 4. Serialize object array
        NSLog(@"\n--- 4. Serialize Object Array ---");

        Person *p1 = [[Person alloc] init];
        p1.name = @"Bob";
        p1.email = @"[email protected]";
        p1.age = 25;
        p1.hobbies = @[@"Gaming"];

        Person *p2 = [[Person alloc] init];
        p2.name = @"Charlie";
        p2.email = @"[email protected]";
        p2.age = 35;
        p2.hobbies = @[@"Cooking", @"Traveling"];

        NSArray *people = @[p1, p2];
        NSData *peopleJSON = [CustomObjectSerializer serializePersonArrayToJSON:people];
        if (peopleJSON) {
            NSString *jsonString = [[NSString alloc] initWithData:peopleJSON
                                                          encoding:NSUTF8StringEncoding];
            NSLog(@"People Array JSON:\n%@", jsonString);
        }

        // 5. Serialize nested objects
        NSLog(@"\n--- 5. Serialize Nested Objects ---");

        Company *company = [[Company alloc] init];
        company.name = @"Tech Corp";
        company.location = @"San Francisco, CA";

        Person *emp1 = [[Person alloc] init];
        emp1.name = @"David";
        emp1.email = @"[email protected]";
        emp1.age = 28;
        emp1.hobbies = @[@"Coding"];

        Person *emp2 = [[Person alloc] init];
        emp2.name = @"Emma";
        emp2.email = @"[email protected]";
        emp2.age = 32;
        emp2.hobbies = @[@"Design", @"Music"];

        company.employees = @[emp1, emp2];

        NSData *companyJSON = [NestedObjectSerializer serializeCompanyToJSON:company];
        if (companyJSON) {
            NSString *jsonString = [[NSString alloc] initWithData:companyJSON
                                                          encoding:NSUTF8StringEncoding];
            NSLog(@"Company JSON:\n%@", jsonString);
        }

        NSLog(@"\n=== JSON Serialization Examples Completed ===");
    }

    return 0;
}
代码objectivec中等

JSON反序列化

使用NSJSONSerialization解析JSON字符串并转换为Objective-C对象

难度

5/10

预计时间

25 min

标签

objectivec, macos, serialization, json

前置条件

Objective-C basics, Foundation framework

objectivec
// macOS Objective-C JSON Deserialization Examples
// Using NSJSONSerialization for JSON decoding

#import <Foundation/Foundation.h>

// MARK: - 1. Deserialize JSON to Dictionary

@interface JSONDeserializer : NSObject

+ (NSDictionary *)deserializeJSONToDictionary:(NSData *)jsonData;
+ (NSDictionary *)deserializeJSONStringToDictionary:(NSString *)jsonString;
+ (NSDictionary *)readJSONFileToDictionary:(NSString *)path;

@end

@implementation JSONDeserializer

+ (NSDictionary *)deserializeJSONToDictionary:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON deserialization error: %@", error.localizedDescription);
        return nil;
    }

    if (![jsonObject isKindOfClass:[NSDictionary class]]) {
        NSLog(@"JSON is not a dictionary");
        return nil;
    }

    NSLog(@"JSON deserialized to dictionary");
    return (NSDictionary *)jsonObject;
}

+ (NSDictionary *)deserializeJSONStringToDictionary:(NSString *)jsonString {
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

    if (!jsonData) {
        NSLog(@"Failed to convert string to data");
        return nil;
    }

    return [self deserializeJSONToDictionary:jsonData];
}

+ (NSDictionary *)readJSONFileToDictionary:(NSString *)path {
    NSError *error = nil;

    NSData *jsonData = [NSData dataWithContentsOfFile:path
                                              options:0
                                                error:&error];

    if (error) {
        NSLog(@"File read error: %@", error.localizedDescription);
        return nil;
    }

    return [self deserializeJSONToDictionary:jsonData];
}

@end

// MARK: - 2. Deserialize JSON to Array

@interface JSONArrayDeserializer : NSObject

+ (NSArray *)deserializeJSONToArray:(NSData *)jsonData;
+ (NSArray *)deserializeJSONStringToArray:(NSString *)jsonString;

@end

@implementation JSONArrayDeserializer

+ (NSArray *)deserializeJSONToArray:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON deserialization error: %@", error.localizedDescription);
        return nil;
    }

    if (![jsonObject isKindOfClass:[NSArray class]]) {
        NSLog(@"JSON is not an array");
        return nil;
    }

    NSLog(@"JSON deserialized to array");
    return (NSArray *)jsonObject;
}

+ (NSArray *)deserializeJSONStringToArray:(NSString *)jsonString {
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

    if (!jsonData) {
        NSLog(@"Failed to convert string to data");
        return nil;
    }

    return [self deserializeJSONToArray:jsonData];
}

@end

// MARK: - 3. Deserialize to Custom Object

@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSArray *hobbies;

+ (instancetype)fromDictionary:(NSDictionary *)dict;
- (NSString *)description;

@end

@implementation Person

+ (instancetype)fromDictionary:(NSDictionary *)dict {
    Person *person = [[Person alloc] init];

    person.name = dict[@"name"];
    person.email = dict[@"email"];

    id ageValue = dict[@"age"];
    if ([ageValue isKindOfClass:[NSNumber class]]) {
        person.age = [ageValue integerValue];
    }

    id hobbiesValue = dict[@"hobbies"];
    if ([hobbiesValue isKindOfClass:[NSArray class]]) {
        person.hobbies = (NSArray *)hobbiesValue;
    } else {
        person.hobbies = @[];
    }

    return person;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"Person[name=%@, email=%@, age=%ld, hobbies=%@]",
            self.name, self.email, (long)self.age, self.hobbies];
}

@end

@interface CustomObjectDeserializer : NSObject

+ (Person *)deserializePersonFromJSON:(NSData *)jsonData;
+ (NSArray *)deserializePersonArrayFromJSON:(NSData *)jsonData;

@end

@implementation CustomObjectDeserializer

+ (Person *)deserializePersonFromJSON:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON deserialization error: %@", error.localizedDescription);
        return nil;
    }

    if (![jsonObject isKindOfClass:[NSDictionary class]]) {
        NSLog(@"JSON is not a dictionary");
        return nil;
    }

    Person *person = [Person fromDictionary:(NSDictionary *)jsonObject];
    NSLog(@"Person deserialized from JSON");

    return person;
}

+ (NSArray *)deserializePersonArrayFromJSON:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON deserialization error: %@", error.localizedDescription);
        return nil;
    }

    if (![jsonObject isKindOfClass:[NSArray class]]) {
        NSLog(@"JSON is not an array");
        return nil;
    }

    NSArray *jsonArray = (NSArray *)jsonObject;
    NSMutableArray *people = [NSMutableArray arrayWithCapacity:jsonArray.count];

    for (NSDictionary *dict in jsonArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [people addObject:[Person fromDictionary:dict]];
        }
    }

    NSLog(@"Person array deserialized from JSON (%lu people)", (unsigned long)people.count);
    return [people copy];
}

@end

// MARK: - 4. Handle Invalid JSON

@interface JSONValidator : NSObject

+ (BOOL)isValidJSON:(NSData *)jsonData;
+ (NSString *)getValidationError:(NSData *)jsonData;

@end

@implementation JSONValidator

+ (BOOL)isValidJSON:(NSData *)jsonData {
    NSError *error = nil;

    [NSJSONSerialization JSONObjectWithData:jsonData
                                    options:0
                                      error:&error];

    return error == nil;
}

+ (NSString *)getValidationError:(NSData *)jsonData {
    NSError *error = nil;

    [NSJSONSerialization JSONObjectWithData:jsonData
                                    options:0
                                      error:&error];

    return error ? error.localizedDescription : nil;
}

@end

// MARK: - 5. Parse Mixed JSON Structures

@interface MixedJSONParser : NSObject

+ (void)parseMixedJSON:(NSData *)jsonData;

@end

@implementation MixedJSONParser

+ (void)parseMixedJSON:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON parsing error: %@", error.localizedDescription);
        return;
    }

    if ([jsonObject isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *)jsonObject;
        NSLog(@"Parsed dictionary with %lu keys", (unsigned long)dict.count);

        // Iterate through dictionary
        [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSLog(@"  Key: %@, Value: %@", key, obj);

            if ([obj isKindOfClass:[NSArray class]]) {
                NSLog(@"    -> Array with %lu items", (unsigned long)[obj count]);
            } else if ([obj isKindOfClass:[NSDictionary class]]) {
                NSLog(@"    -> Nested dictionary");
            }
        }];

    } else if ([jsonObject isKindOfClass:[NSArray class]]) {
        NSArray *array = (NSArray *)jsonObject;
        NSLog(@"Parsed array with %lu items", (unsigned long)array.count);
    }
}

@end

// MARK: - Main Demonstration

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"=== macOS Objective-C JSON Deserialization Examples ===\n");

        // 1. Deserialize to dictionary
        NSLog(@"--- 1. Deserialize to Dictionary ---");

        NSString *userJSONString = @"{\"id\": 1,\"username\": \"john_doe\", \"email\": \"[email protected]\", \"active\": true}";

        NSDictionary *userDict = [JSONDeserializer deserializeJSONStringToDictionary:userJSONString];
        if (userDict) {
            NSLog(@"Username: %@", userDict[@"username"]);
            NSLog(@"Email: %@", userDict[@"email"]);
            NSLog(@"Active: %@", userDict[@"active"]);
        }

        // 2. Deserialize to array
        NSLog(@"\n--- 2. Deserialize to Array ---");

        NSString *fruitsJSONString = @"[\"Apple\", \"Banana\", \"Orange\", \"Mango\"]";

        NSArray *fruits = [JSONArrayDeserializer deserializeJSONStringToArray:fruitsJSONString];
        if (fruits) {
            NSLog(@"Fruits: %@", fruits);
        }

        // 3. Deserialize to custom object
        NSLog(@"\n--- 3. Deserialize to Custom Object ---");

        NSString *personJSONString = @"{\"name\": \"Alice Smith\", \"email\": \"[email protected]\", \"age\": 30, \"hobbies\": [\"Reading\", \"Hiking\"]}";

        NSData *personJSONData = [personJSONString dataUsingEncoding:NSUTF8StringEncoding];
        Person *person = [CustomObjectDeserializer deserializePersonFromJSON:personJSONData];
        if (person) {
            NSLog(@"%@", person);
        }

        // 4. Deserialize to object array
        NSLog(@"\n--- 4. Deserialize to Object Array ---");

        NSString *peopleJSONString = @"["
            @"{\"name\": \"Bob\", \"email\": \"[email protected]\", \"age\": 25, \"hobbies\": [\"Gaming\"]},"
            @"{\"name\": \"Charlie\", \"email\": \"[email protected]\", \"age\": 35, \"hobbies\": [\"Cooking\", \"Traveling\"]}"
        @"]";

        NSData *peopleJSONData = [peopleJSONString dataUsingEncoding:NSUTF8StringEncoding];
        NSArray *people = [CustomObjectDeserializer deserializePersonArrayFromJSON:peopleJSONData];
        for (Person *p in people) {
            NSLog(@"%@", p);
        }

        // 5. Validate JSON
        NSLog(@"\n--- 5. Validate JSON ---");

        NSString *validJSON = @"{\"name\": \"Test\", \"value\": 123}";
        NSString *invalidJSON = @"{\"name\": \"Test\", \"value\": 123";

        NSData *validData = [validJSON dataUsingEncoding:NSUTF8StringEncoding];
        NSData *invalidData = [invalidJSON dataUsingEncoding:NSUTF8StringEncoding];

        NSLog(@"Valid JSON: %d", [JSONValidator isValidJSON:validData]);
        NSLog(@"Invalid JSON: %d", [JSONValidator isValidJSON:invalidData]);

        NSString *error = [JSONValidator getValidationError:invalidData];
        if (error) {
            NSLog(@"Error: %@", error);
        }

        // 6. Parse mixed structures
        NSLog(@"\n--- 6. Parse Mixed Structures ---");

        NSString *mixedJSON = @"{"
            @"\"company\": \"Tech Corp\","
            @"\"employees\": ["
            @"  {\"name\": \"Alice\", \"role\": \"Engineer\"},"
            @"  {\"name\": \"Bob\", \"role\": \"Designer\"}"
            @"],"
            @"\"metadata\": {"
            @"   \"founded\": 2010,"
            @"   \"location\": \"San Francisco\""
            @"}"
        @"}";

        NSData *mixedData = [mixedJSON dataUsingEncoding:NSUTF8StringEncoding];
        [MixedJSONParser parseMixedJSON:mixedData];

        NSLog(@"\n=== JSON Deserialization Examples Completed ===");
    }

    return 0;
}
代码objectivec中等

XML解析

使用NSXMLParser委托模式解析XML文档

难度

6/10

预计时间

30 min

标签

objectivec, macos, serialization, xml

前置条件

Objective-C basics, Foundation framework, Delegate pattern

objectivec
// macOS Objective-C XML Parsing Examples
// Using NSXMLParser delegate pattern

#import <Foundation/Foundation.h>

// MARK: - 1. Simple XML Parser

@interface SimpleXMLParser : NSObject <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableString *currentElementValue;
@property (nonatomic, strong) NSMutableArray *parsedItems;
@property (nonatomic, strong) NSMutableDictionary *currentItem;

- (void)parseXMLFromString:(NSString *)xmlString;
- (void)parseXMLFromFile:(NSString *)filePath;

@end

@implementation SimpleXMLParser

- (instancetype)init {
    self = [super init];

    if (self) {
        _currentElementValue = [[NSMutableString alloc] init];
        _parsedItems = [[NSMutableArray alloc] init];
    }

    return self;
}

- (void)parseXMLFromString:(NSString *)xmlString {
    NSData *xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
    parser.delegate = self;

    BOOL success = [parser parse];

    if (success) {
        NSLog(@"XML parsing successful, parsed %lu items", (unsigned long)self.parsedItems.count);
    } else {
        NSLog(@"XML parsing failed: %@", parser.parserError.localizedDescription);
    }
}

- (void)parseXMLFromFile:(NSString *)filePath {
    NSData *xmlData = [NSData dataWithContentsOfFile:filePath];

    if (!xmlData) {
        NSLog(@"Failed to read XML file");
        return;
    }

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
    parser.delegate = self;

    BOOL success = [parser parse];

    if (success) {
        NSLog(@"XML parsing successful, parsed %lu items", (unsigned long)self.parsedItems.count);
    } else {
        NSLog(@"XML parsing failed: %@", parser.parserError.localizedDescription);
    }
}

// NSXMLParserDelegate methods

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary<NSString *, NSString *> *)attributeDict {

    [self.currentElementValue setString:@""];

    if ([elementName isEqualToString:@"item"]) {
        self.currentItem = [[NSMutableDictionary alloc] init];
    }

    NSLog(@"Started element: %@", elementName);

    // Log attributes
    [attributeDict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
        NSLog(@"  Attribute: %@ = %@", key, value);
    }];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [self.currentElementValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName {

    NSString *trimmedValue = [self.currentElementValue stringByTrimmingCharactersInSet:
                             [NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([elementName isEqualToString:@"item"]) {
        if (self.currentItem) {
            [self.parsedItems addObject:self.currentItem];
            self.currentItem = nil;
        }
    } else if (self.currentItem && trimmedValue.length > 0) {
        [self.currentItem setObject:trimmedValue forKey:elementName];
    }

    NSLog(@"Ended element: %@ = %@", elementName, trimmedValue);
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"XML document parsing completed");
    NSLog(@"Parsed items: %@", self.parsedItems);
}

@end

// MARK: - 2. Catalog XML Parser

@interface Book : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *author;
@property (nonatomic, strong) NSString *genre;
@property (nonatomic, assign) CGFloat price;
@property (nonatomic, assign) NSInteger publishDate;
@property (nonatomic, strong) NSString *description;

- (NSString *)description;

@end

@implementation Book

- (NSString *)description {
    return [NSString stringWithFormat:@"Book[title=%@, author=%@, genre=%@, price=%.2f, year=%ld]",
            self.title, self.author, self.genre, self.price, (long)self.publishDate];
}

@end

@interface CatalogXMLParser : NSObject <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableArray *books;
@property (nonatomic, strong) Book *currentBook;
@property (nonatomic, strong) NSMutableString *currentText;

- (NSArray *)parseCatalogXML:(NSString *)xmlString;

@end

@implementation CatalogXMLParser

- (instancetype)init {
    self = [super init];

    if (self) {
        _books = [[NSMutableArray alloc] init];
        _currentText = [[NSMutableString alloc] init];
    }

    return self;
}

- (NSArray *)parseCatalogXML:(NSString *)xmlString {
    NSData *xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
    parser.delegate = self;

    if ([parser parse]) {
        NSLog(@"Catalog parsed successfully, %lu books", (unsigned long)self.books.count);
        return [self.books copy];
    }

    NSLog(@"Parsing failed: %@", parser.parserError.localizedDescription);
    return nil;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict {

    [self.currentText setString:@""];

    if ([elementName isEqualToString:@"book"]) {
        self.currentBook = [[Book alloc] init];

        // Extract attributes from book element
        NSString *idValue = attributeDict[@"id"];
        NSLog(@"Parsing book with ID: %@", idValue);
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [self.currentText appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName {

    NSString *trimmedText = [self.currentText stringByTrimmingCharactersInSet:
                            [NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([elementName isEqualToString:@"book"]) {
        if (self.currentBook) {
            [self.books addObject:self.currentBook];
            self.currentBook = nil;
        }
    } else if (self.currentBook) {
        if ([elementName isEqualToString:@"title"]) {
            self.currentBook.title = trimmedText;
        } else if ([elementName isEqualToString:@"author"]) {
            self.currentBook.author = trimmedText;
        } else if ([elementName isEqualToString:@"genre"]) {
            self.currentBook.genre = trimmedText;
        } else if ([elementName isEqualToString:@"price"]) {
            self.currentBook.price = [trimmedText floatValue];
        } else if ([elementName isEqualToString:@"publish_date"]) {
            // Parse year from date format
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyy-MM-dd";
            NSDate *date = [formatter dateFromString:trimmedText];

            if (date) {
                NSCalendar *calendar = [NSCalendar currentCalendar];
                NSDateComponents *components = [calendar components:NSCalendarUnitYear
                                                            fromDate:date];
                self.currentBook.publishDate = components.year;
            }
        } else if ([elementName isEqualToString:@"description"]) {
            self.currentBook.description = trimmedText;
        }
    }
}

@end

// MARK: - 3. RSS Feed Parser

@interface RSSFeedParser : NSObject <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, strong) NSMutableDictionary *currentItem;
@property (nonatomic, strong) NSMutableString *currentElementValue;
@property (nonatomic, strong) NSMutableString *currentElementName;

@property (nonatomic, strong) NSString *feedTitle;
@property (nonatomic, strong) NSString *feedDescription;
@property (nonatomic, strong) NSString *feedLink;

- (NSDictionary *)parseRSSFeed:(NSString *)rssString;

@end

@implementation RSSFeedParser

- (instancetype)init {
    self = [super init];

    if (self) {
        _items = [[NSMutableArray alloc] init];
        _currentElementValue = [[NSMutableString alloc] init];
        _currentElementName = [[NSMutableString alloc] init];
    }

    return self;
}

- (NSDictionary *)parseRSSFeed:(NSString *)rssString {
    NSData *rssData = [rssString dataUsingEncoding:NSUTF8StringEncoding];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:rssData];
    parser.delegate = self;

    if ([parser parse]) {
        NSLog(@"RSS feed parsed, %lu items", (unsigned long)self.items.count);

        return @{
            @"title": self.feedTitle ?: @"",
            @"description": self.feedDescription ?: @"",
            @"link": self.feedLink ?: @"",
            @"items": [self.items copy]
        };
    }

    return nil;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict {

    [self.currentElementName setString:elementName];
    [self.currentElementValue setString:@""];

    if ([elementName isEqualToString:@"item"]) {
        self.currentItem = [[NSMutableDictionary alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [self.currentElementValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName {

    NSString *value = [self.currentElementValue stringByTrimmingCharactersInSet:
                      [NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([elementName isEqualToString:@"item"]) {
        if (self.currentItem) {
            [self.items addObject:self.currentItem];
            self.currentItem = nil;
        }
    } else if ([elementName isEqualToString:@"title"] && !self.currentItem) {
        self.feedTitle = value;
    } else if ([elementName isEqualToString:@"description"] && !self.currentItem) {
        self.feedDescription = value;
    } else if ([elementName isEqualToString:@"link"] && !self.currentItem) {
        self.feedLink = value;
    } else if (self.currentItem && value.length > 0) {
        [self.currentItem setObject:value forKey:elementName];
    }
}

@end

// MARK: - Main Demonstration

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"=== macOS Objective-C XML Parsing Examples ===\n");

        // 1. Simple XML parsing
        NSLog(@"--- 1. Simple XML Parsing ---");

        NSString *simpleXML = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            @"<data>"
            @"  <item>"
            @"    <name>Apple</name>"
            @"    <price>1.50</price>"
            @"    <quantity>10</quantity>"
            @"  </item>"
            @"  <item>"
            @"    <name>Banana</name>"
            @"    <price>0.75</price>"
            @"    <quantity>20</quantity>"
            @"  </item>"
            @"</data>";

        SimpleXMLParser *simpleParser = [[SimpleXMLParser alloc] init];
        [simpleParser parseXMLFromString:simpleXML];

        // 2. Catalog XML parsing
        NSLog(@"\n--- 2. Catalog XML Parsing ---");

        NSString *catalogXML = @"<?xml version=\"1.0\"?>"
            @"<catalog>"
            @"  <book id=\"bk101\">"
            @"    <author>Gambardella, Matthew</author>"
            @"    <title>XML Developer's Guide</title>"
            @"    <genre>Computer</genre>"
            @"    <price>44.95</price>"
            @"    <publish_date>2000-10-01</publish_date>"
            @"    <description>An in-depth look at creating applications with XML.</description>"
            @"  </book>"
            @"  <book id=\"bk102\">"
            @"    <author>Ralls, Kim</author>"
            @"    <title>Midnight Rain</title>"
            @"    <genre>Fantasy</genre>"
            @"    <price>5.95</price>"
            @"    <publish_date>2000-12-16</publish_date>"
            @"    <description>A former architect battles corporate zombies.</description>"
            @"  </book>"
            @"</catalog>";

        CatalogXMLParser *catalogParser = [[CatalogXMLParser alloc] init];
        NSArray *books = [catalogParser parseCatalogXML:catalogXML];

        for (Book *book in books) {
            NSLog(@"%@", book);
        }

        // 3. RSS feed parsing
        NSLog(@"\n--- 3. RSS Feed Parsing ---");

        NSString *rssFeed = @"<?xml version=\"1.0\"?>"
            @"<rss version=\"2.0\">"
            @"  <channel>"
            @"    <title>Tech News</title>"
            @"    <description>Latest technology news</description>"
            @"    <link>https://example.com</link>"
            @"    <item>"
            @"      <title>New iPhone Released</title>"
            @"      <link>https://example.com/iphone</link>"
            @"      <description>Apple announces latest iPhone model</description>"
            @"      <pubDate>Mon, 25 Dec 2025 10:00:00 GMT</pubDate>"
            @"    </item>"
            @"    <item>"
            @"      <title>AI Breakthrough</title>"
            @"      <link>https://example.com/ai</link>"
            @"      <description>Scientists achieve major AI milestone</description>"
            @"      <pubDate>Sun, 24 Dec 2025 15:30:00 GMT</pubDate>"
            @"    </item>"
            @"  </channel>"
            @"</rss>";

        RSSFeedParser *rssParser = [[RSSFeedParser alloc] init];
        NSDictionary *feed = [rssParser parseRSSFeed:rssFeed];

        NSLog(@"Feed Title: %@", feed[@"title"]);
        NSLog(@"Feed Description: %@", feed[@"description"]);
        NSLog(@"\nFeed Items:");

        for (NSDictionary *item in feed[@"items"]) {
            NSLog(@"  - %@ (%@)", item[@"title"], item[@"pubDate"]);
        }

        NSLog(@"\n=== XML Parsing Examples Completed ===");
    }

    return 0;
}

工具

经常与该案例搭配使用的工具

相关内容

相关案例集合

相关内容

相关专题

工具使用指南

帮助搜索与理解工作流的案例说明

概览

三个练习分别隔离不同帮助函数,全部使用虚构值,不读取任何人、浏览器或服务器的信息。

阅读 macOS 文件、序列化及并发帮助函数

不要启动完整入口。不操作设备、网络、权限、身份验证、浏览器、文件或回调。整张卡片仍可能需要依赖、语法和集成修复。

阅读 macOS 文件、序列化及并发帮助函数
卡片阅读练习
JSON序列化null 与空数组不同
JSON反序列化拒绝不同的根结构
XML解析累积拆分的字符片段

使用方式

  1. 1选择代码卡片,追踪固定输入,区分本地计算与真实设备或服务器行为。

常见失败与 edge cases

  • 范围与限制

    不要启动完整入口。不操作设备、网络、权限、身份验证、浏览器、文件或回调。整张卡片仍可能需要依赖、语法和集成修复。

示例

1. null 与空数组不同

如何使用

在《JSON序列化》纸面追踪 Person.toDictionary,代入:name=nil; email=nil; age=0; hobbies=nil。

结果

预期源码推演:{name:NSNull, email:NSNull, age:0, hobbies:[]}. 在序列化之前追踪 Person.toDictionary。name、email 缺失使用 NSNull,hobbies 缺失使用空数组,四个键都保留;不提供个人数据。

2. 拒绝不同的根结构

如何使用

在《JSON反序列化》纸面追踪 deserializeJSONToDictionary:,代入:error=nil; jsonObject=[]; then jsonObject={k:1}。

结果

预期源码推演:[] → nil; {k:1} → {k:1}. 直接代入解析成功后的结果,不解析字节。有效 JSON 数组仍被字典帮助函数的根类型检查拒绝;语法有效并不足够。

3. 累积拆分的字符片段

如何使用

在《XML解析》纸面追踪 foundCharacters:; didEndElement:,代入:currentItem={}; elementName=title; chunks=" Al", "pha "。

结果

预期源码推演:currentItem={title:"Alpha"}. 这些是虚构回调输入,不是 XML 解析。元素开始时清空,追加两个片段,结束时再统一去空白。不测试 DTD、外部实体、嵌套混合内容或文件输入。

专题

容器发布前的 Dockerfile 与环境配置审查

检查 Dockerfile 规则、镜像引用格式与环境变量重复键,解释预发布和生产配置差异,形成可核对的容器发布记录。

专题

域名上线前的 DNS、DNSSEC 与 HTTPS 验收

先拟定 DNS 区域记录,再核对在线解析、DNSSEC 委派和实际提供的 HTTPS 证书,为域名上线留下可复核的依据。

专题

重复日历与 ICS 日程安排

跨时区规划并验证重复会议,处理工作日、节假日、冲突和 ICS 导出要求。

专题

API Mock 夹具生成与审阅

生成可重复的 Mock 负载,构造符合格式的边界值,发现易混淆字段名,并在共享临时 Mock 服务前完成复核。

首页
工具3216
案例593
可视化473
文件查看器103
专题239
标签333
图像、音频与视频657

图像、音频、视频处理、转换与优化工具

数学、日期与金融647

计算器、数值计算、日期逻辑、统计与金融工具

设计与颜色298

颜色、版式、图形、视觉样式与设计辅助工具

文本与写作209

写作、清洗、格式化、提取与文本分析工具

开发与 Web194

开发者工具、网络处理、Web 调试与自动化辅助工具

转换与编码175

格式、文件、压缩包、单位与编码转换工具

潜客测验发布与联系人交接
讣告、悼词与纪念卡的家属复核及交付
本地习惯记录的备份、恢复演练与分享前检查
长文改编 X 串文与 LinkedIn 帖子的事实复核与交付
Markdown 互动课件审校、离线演示与交付

© 2026 Elysia Tools. 保留所有权利。

文档价格关于我们服务条款隐私政策Cookie 政策联系我们

Format Conversion

XML转JSON转换器

将XML数据转换为JSON格式,支持自定义解析选项

Development

游戏存档查看器

拖入任意游戏存档——Minecraft NBT、虚幻引擎 GVAS、幻兽帕鲁容器、JSON/XML/INI/Lua 文本存档、SQLite 档案——识别格式、引擎版本、关键进度字段并给出有界的结构树。

Format Conversion

JSON转XML转换器

将JSON数据转换为XML格式,支持自定义格式化选项

Development

SBOM SPDX / CycloneDX 许可证策略审计器

审计 SPDX 2.3 或 CycloneDX 1.5 组件许可证,输出 SARIF 和失败依赖链

Format Conversion

XLSX 迷你图注入器

通过修改工作表 XML 向 Excel 单元格中插入 Sparkline 迷你图

Data Processing

JSON键提取器

从JSON对象中提取所有键,支持多种输出格式。非常适合分析JSON结构、生成文档和理解复杂的嵌套对象。

SEO

CSV 生成 JSON-LD

把 CSV 或 Excel 行数据转换为适用于 Article、Product、Event 的 Schema.org JSON-LD,方便 SEO 团队直接验证

Data Analysis

时间序列异常检测器

上传 CSV 或 JSON 时间序列数据,基于 Z-Score 和 IQR 检测异常点,并输出带图表的报告

C++

Windows C++ 序列化示例

Windows C++ 数据序列化示例,包括JSON序列化/反序列化和XML解析

Java

Android Java 序列化示例

Android Java 序列化示例,包括JSON序列化、反序列化和XML解析

Kotlin

Android Kotlin 序列化示例

Android Kotlin 序列化示例,包括JSON序列化、反序列化和XML解析

Go

Web Go 数据序列化示例

Web Go 数据序列化示例,包括JSON编码/解码和XML解析

Python

Web Python 数据序列化示例

Web Python 数据序列化示例,包括JSON和XML处理

Rust

Web Rust 数据序列化示例

Web Rust 数据序列化示例,包括JSON序列化、反序列化和XML解析