• An overview of Struct

  • Advantages and disadvantages of structs relative to classes

  • Convert Class to Swift

  • Struct with ObjectMapper

Novices to Swift are often seen trying to translate their ObjC code to Swift. But the hardest thing about starting to code in Swift isn't the syntax, it's the mindset shift to use new concepts of Swift that aren't in ObjC.Copy the code

1. Overview of Struct

Struct is a value type and class is a reference type. Variables of a value type directly contain their data, while variables of a reference type store references to their data, so the latter are called objects, so operations on one variable may affect objects referenced by another variable. Each value type has its own copy of the data, so operations on one variable cannot affect another. Structs in Swift are much more powerful than structs in OC, with not only member variables but also member methods, making it more like a class.

2. Advantages and disadvantages of Struct relative to Class

advantages

Because structs are passed as value types, they have no reference count. There is no need to worry about data being changed by other holdersCopy the code
Since there is no reference count, there is no memory leak due to circular references.Copy the code
Value types are usually allocated on a stack, not a heap. So they are much faster than Class, really much faster! In [StackOverflow] (http://stackoverflow.com/a/24243626/596821) for the struct and class for the same operation performance benchmarks, struct nine hundred times faster than the class. (note: in the 2016-05-07 tests, the speed of the Struct for Class thirty-seven million times, see [StackOverflow] (http://stackoverflow.com/a/24243626/596821))Copy the code
Because it is a value type, there is no need to think of deep and shallow copies like reference types. Copying value types is very easy!Copy the code
Value types are automatically thread-safe. No matter which thread you're accessing your Struct from, it's pretty simple.Copy the code
struct SListItem { var icon: String = "" var title: String = "" var url: String = ""} func start() {// Let slist:SListItem = SListItem(icon: "", title: "", url: "") let clist:CListItem = CListItem() } class CListItem{ var icon: String = "" var title: String = "" var url: String = "" }Copy the code

disadvantages

In mixed development,Swift's Struct cannot be called by OC, because to call Swift code in Objective-C, the object needs to inherit from NSObject.Copy the code
As one of the three object-oriented features, inheritance allows developers to save most of the repetitive code, but structs do not support inheritance. Fortunately, structs support protocols, which also reflects Swift's protocol-oriented programming philosophyCopy the code
Structs cannot be serialized into NSData objects. NSUserDefaults, for example, you still have to use classCopy the code

3. Convert Class to Swift

In our case, using a structure seems more appropriate because it holds some values and doesn’t require any changes to them (more suitable for copying than referencing). In our case, we use it as a data source for a menu bar, and once created it is not changed, so this is a more logical scenario to use a structure.

struct ListItem {
    var icon: UIImage?
    var title: String
    var url: NSURL
    static func listItemsFromJSONData(jsonData: NSData?) -> [ListItem] {
        guard let nonNilJsonData = jsonData,
            let json = try? JSONSerialization.jsonObject(with: nonNilJsonData as Data, options: []),
            let jsonItems = json as? Array<NSDictionary> else { return [] }
        return jsonItems.flatMap { (itemDesc: NSDictionary) -> ListItem? in
            guard let title = itemDesc["title"] as? String,
                let urlString = itemDesc["url"] as? String,
                let url = NSURL(string: urlString)
                else { return nil }
            let iconName = itemDesc["icon"] as? String
            let icon = UIImage(named: iconName ?? "")
            return ListItem(icon: icon, title: title, url: url)
        }
    }
}
Copy the code

4. The Struct with ObjectMapper

In the process of dictionary to model, we can use ObjectMapper to operateCopy the code
The article refers to a lot of domestic and foreign articles, plus some personal views ability is very little, only dare to say pieced together if there is insufficient, please adviseCopy the code

Article reference :medium.com/swift-progr… Swift. Gg / 2015/10/20 /…