NSData NSDate NSString NSArray NSDictionary mutual conversion, nsdatansdate
// NSData NSDate NSString NSArray NSDictionary json NSString * string = @ "hello word"; NSData * data = [string dataUsingEncoding: NSUTF8StringEncoding]; NSArray * array = [NSArray arrayWithObjects: @ "1", @ "2", @ "3", nil]; NSString * dateString = @ "19891123"; NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "hello ", @ "word", @ "chen", @ "lu", @ "jin", @ "mei", @ "cerastes", @ "hehe", nil]; // NSString to NSData * string2data = [string dataUsingEncoding: NSUTF8StringEncoding]; NSLog (@ "% @", string2data ); // NSData to NSString * data2String = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "dataString = % @", data2String ); // NSArray to NSData * arry2data = [NSKeyedArchiver failed: array]; // NSData to NSArray * data2arry = [NSKeyedUnarchiver unarchiveObjectWithData: arry2data]; NSLog (@ "% @/n % @", arry2data, data2arry); // NSString to NSDate * string2date = [self convertDateFromString: dateString withFormattre: @ "yyyyMMdd"]; NSLog (@ "date = % @", string2date); // NSDate to NSString NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; NSString * date2string = [dateFormatter stringFromDate: [NSDate date]; NSLog (@ "% @", date2string ); // NSArray to NSString * arry2String = [array componentsJoinedByString: @ ""]; NSLog (@ "string = % @", arry2String ); // NSString to NSArray * string2Arry = [arry2String componentsSeparatedByString: @ ""]; NSLog (@ "% @", string2Arry); // NSDictionary to NSData: NSMutableData * dic2data = [[NSMutableData alloc] init]; NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] metric: dic2data]; [archiver encodeObject: dic forKey: @ "Some Key Value"]; [archiver finishEncoding]; NSLog (@ "% @", dic2data); // NSData to NSDictionary convert * unarchiver = [NSKeyedUnarchiver alloc] initForReadingWithData: dic2data]; NSDictionary * data2dic = [unarchiver decodeObjectForKey: @ "Some Key Value"]; [unarchiver finishDecoding]; NSLog (@ "% @", data2dic ); // NSDictionary to NSString * dic2string = [dic JSONRepresentation]; // This method is in the SBJson class library and needs to be imported into SBJson. h NSLog (@ "% @", dic2string); // NSString to NSDictionary NSError * error = nil; NSDictionary * string2dic = [NSJSONSerialization JSONObjectWithData: [dic2string encoding: encoding] options: NSJSONReadingMutableContainers error: & error]; NSLog (@ "% @", string2dic );
How to use the design mode of Cocoa
Cocoa often builds its own distinctive working mechanism on patterns, and its design is influenced by factors such as language capabilities or existing architecture. This section contains the design patterns: An Introduction to most programming patterns in the book elements of reusable object-oriented software. Each design pattern has a summative description and a discussion of the Cocoa implementation of this pattern. All the modes listed in this article are Cocoa implementation modes. The discussion of each mode occurs in a specific Cocoa environment. We recommend that you familiarize yourself with these modes, and you will find them useful in Cocoa software development. The implementation of the Design Pattern in Cocoa has different forms. Some designs described in the following sections-such as protocols and categories-are Objective-c features; in other scenarios, the "pattern instance" is implemented as a class or a group of related classes (such as class clusters and single-piece classes). In other cases, the pattern is represented as a large framework structure, for example, the responder chain mode. For some mode-based mechanisms, you can use them for almost "free", while other mechanisms require you to do some work. Even if the Cocoa model is not implemented, we encourage you to implement it by yourself as conditions permit, for example, object merging (decoration mode) when extending the behavior of classes) technology is generally better than subclass generation. Two design patterns are not discussed in the following content: Model-View-controller (MVC) mode and object modeling. MVC is a combination or aggregation mode, that is, it is based on several different types of patterns. Object Modeling does not have a corresponding category in the category directory of a four-person group. It originated from the relational database field. However, MVC and Object Modeling may be the most important and common design patterns or terms in Cocoa, and they are largely related. They play a key role in the design of several technologies, including binding, Revocation Management, script control, and document architecture. For more information about these modes, see "Model-View-controller design mode" and "Object Modeling. This section includes the following main content: abstract Factory mode adapter mode responsibility chain mode command mode synthesis mode decoration mode apparent mode drop substitution mode Arbitration mode memorandum Mode Observer mode proxy mode single-piece mode template method mode Abstract Factory mode provides an interface, creates a class family that is related to or dependent on some objects without specifying their specific classes. This mode removes the coupling between the customer code and the details of specific objects from the factory. A class cluster is an architecture that combines private and specific sub-classes under a common abstract super class. Abstract superclass are responsible for declaring the method for creating a private subclass instance. Appropriate concrete subclasses are allocated based on different called methods. Each returned object may belong to different private subclasses. Cocoa limits class clusters to the generation of objects that may change due to the environment of data storage. The Foundation framework defines class clusters for NSString, NSData, NSDictionary, NSSet, and NSArray objects. Common superclass include the aforementioned immutable classes and their complementary variable classes NSMutableString, NSMutableData, NSMutableDictionary, NSMutableSet, and NSMutableArray. Use and limit when you want to create a mutable or immutable object of the type represented by a Class cluster, you can use a public class in the class cluster. The use of class clusters is a compromise between simplicity and scalability. A class cluster can simplify the class interface, so it is easier to learn and use. However, it is more difficult to create a class abstract class. For more information about Cocoa clusters, see "clusters. The adapter mode converts a class interface to another interface required by the customer code. The adapter allows classes that cannot work together due to compatibility, eliminating coupling between the client code and the class of the target object. Protocol protocol is a programming language-level (Objective-C) feature that makes it possible to define an instance of the adapter mode (the "interface" and "protocol" in Java are synonymous ). If you want one customer to communicate with another, but because they are... the remaining full text>
How can NSIndexPath be saved to NSUserDefaults?
NSIndexPath cannot be directly stored in NSUserDefaults. The value in NSUserDefaults can only be NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to save the custom class, the class must implement the NSCoding protocol and convert the class to NSData before it can be kept in NSUserDefaults ~