Swift 2 Object Mapper class append 不起作用,结果为 nil

Swift 2 Object Mapper class append doesn't work, result nil

我在 Swift 2 中使用对象映射器(当我从 api 下载时它运行良好)但我想在我的 class 中添加一些对象并附加。它目前不起作用,即它不添加任何对象。

我的代码如下:

myHomesClass.swift

import Foundation

class MyHomes : NSObject,Mappable {

    var first                   : String?;
    var second                  : String?;
    var number1                 : String?;
    var selected                : String?;


    override init() {
        super.init()
    }
    required convenience init?(_ map: Map) {
        self.init()
     mapping(map)
    }



    func mapping(_ map: Map) {

        first           <- map["first"]
        second          <- map["second"]
        number1         <- map["number1"]

    }
}

我的ViewController.Swift

    import UIKit
    import Foundation

    class ViewController: UIViewController {


    var homes                      : [MyHomes]?
    var selectedHomes              : [MyHomes]?


            override func viewDidLoad() {
                super.viewDidLoad()

                 if homes != nil {
                    for datasinhomes in homes! {
                        if datasinhomes.selected == "yes" {
                           print("selected yes have")
                           self.selectedHomes?.append(datasinhomes)
                            }
 }
}

                 print("selectedHomesCount=\(self.selectedHomes.count)")

}

    }

结果

selected yes have
selected yes have
selected yes have
selectedHomesCount=nil

Mappable.swift

//
//  Mappable.swift
//  ObjectMapper
//
//  Created by Scott Hoyt on 10/25/15.
//  Copyright © 2015 hearst. All rights reserved.
//

import Foundation

public protocol Mappable {
    init?(_ map: Map)
    mutating func mapping(map: Map)
}

public protocol MappableCluster: Mappable {
    static func objectForMapping(map: Map) -> Mappable?
}

public extension Mappable {

    /// Initializes object from a JSON String
    public init?(JSONString: String) {
        if let obj: Self = Mapper().map(JSONString) {
            self = obj
        } else {
            return nil
        }
    }

    /// Initializes object from a JSON Dictionary
    public init?(JSON: [String : AnyObject]) {
        if let obj: Self = Mapper().map(JSON) {
            self = obj
        } else {
            return nil
        }
    }

    /// Returns the JSON Dictionary for the object
    public func toJSON() -> [String: AnyObject] {
        return Mapper().toJSON(self)
    }

    /// Returns the JSON String for the object
    public func toJSONString(prettyPrint: Bool = false) -> String? {
        return Mapper().toJSONString(self, prettyPrint: prettyPrint)
    }
}

public extension Array where Element: Mappable {

    /// Initialize Array from a JSON String
    public init?(JSONString: String) {
        if let obj: [Element] = Mapper().mapArray(JSONString) {
            self = obj
        } else {
            return nil
        }
    }

    /// Initialize Array from a JSON Array
    public init?(JSONArray: [[String : AnyObject]]) {
        if let obj: [Element] = Mapper().mapArray(JSONArray) {
            self = obj
        } else {
            return nil
        }
    }

    /// Returns the JSON Array
    public func toJSON() -> [[String : AnyObject]] {
        return Mapper().toJSONArray(self)
    }

    /// Returns the JSON String for the object
    public func toJSONString(prettyPrint: Bool = false) -> String? {
        return Mapper().toJSONString(self, prettyPrint: prettyPrint)
    }
}

public extension Set where Element: Mappable {

    /// Initializes a set from a JSON String
    public init?(JSONString: String) {
        if let obj: Set<Element> = Mapper().mapSet(JSONString) {
            self = obj
        } else {
            return nil
        }
    }

    /// Initializes a set from JSON
    public init?(JSONArray: [[String : AnyObject]]) {
        if let obj: Set<Element> = Mapper().mapSet(JSONArray) {
            self = obj
        } else {
            return nil
        }
    }

    /// Returns the JSON Set
    public func toJSON() -> [[String : AnyObject]] {
        return Mapper().toJSONSet(self)
    }

    /// Returns the JSON String for the object
    public func toJSONString(prettyPrint: Bool = false) -> String? {
        return Mapper().toJSONString(self, prettyPrint: prettyPrint)
    }
}

Mapper.swift

//
//  Mapper.swift
//  ObjectMapper
//
//  Created by Tristan Himmelman on 2014-10-09.
//
//  The MIT License (MIT)
//
//  Copyright (c) 2014-2015 Hearst
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

import Foundation

public enum MappingType {
    case FromJSON
    case ToJSON
}

/// The Mapper class provides methods for converting Model objects to JSON and methods for converting JSON to Model objects
public final class Mapper<N: Mappable> {

    public init(){}

    // MARK: Mapping functions that map to an existing object toObject

    /// Maps a JSON object to an existing Mappable object if it is a JSON dictionary, or returns the passed object as is
    public func map(JSON: AnyObject?, toObject object: N) -> N {
        if let JSON = JSON as? [String : AnyObject] {
            return map(JSON, toObject: object)
        }

        return object
    }

    /// Map a JSON string onto an existing object
    public func map(JSONString: String, toObject object: N) -> N {
        if let JSON = Mapper.parseJSONDictionary(JSONString) {
            return map(JSON, toObject: object)
        }
        return object
    }

    /// Maps a JSON dictionary to an existing object that conforms to Mappable.
    /// Usefull for those pesky objects that have crappy designated initializers like NSManagedObject
    public func map(JSONDictionary: [String : AnyObject], var toObject object: N) -> N {
        let map = Map(mappingType: .FromJSON, JSONDictionary: JSONDictionary, toObject: true)
        object.mapping(map)
        return object
    }

    //MARK: Mapping functions that create an object

    /// Map an optional JSON string to an object that conforms to Mappable
    public func map(JSONString: String?) -> N? {
        if let JSONString = JSONString {
            return map(JSONString)
        }

        return nil
    }

    /// Map a JSON string to an object that conforms to Mappable
    public func map(JSONString: String) -> N? {
        if let JSON = Mapper.parseJSONDictionary(JSONString) {
            return map(JSON)
        }

        return nil
    }

    /// Map a JSON NSString to an object that conforms to Mappable
    public func map(JSONString: NSString) -> N? {
        return map(JSONString as String)
    }

    /// Maps a JSON object to a Mappable object if it is a JSON dictionary or NSString, or returns nil.
    public func map(JSON: AnyObject?) -> N? {
        if let JSON = JSON as? [String : AnyObject] {
            return map(JSON)
        }

        return nil
    }

    /// Maps a JSON dictionary to an object that conforms to Mappable
    public func map(JSONDictionary: [String : AnyObject]) -> N? {
        let map = Map(mappingType: .FromJSON, JSONDictionary: JSONDictionary)

        // check if N is of type MappableCluster
        if let klass = N.self as? MappableCluster.Type {
            if var object = klass.objectForMapping(map) as? N {
                object.mapping(map)
                return object
            }
        }

        if var object = N(map) {
            object.mapping(map)
            return object
        }
        return nil
    }

    // MARK: Mapping functions for Arrays and Dictionaries

    /// Maps a JSON array to an object that conforms to Mappable
    public func mapArray(JSONString: String) -> [N]? {
        let parsedJSON: AnyObject? = Mapper.parseJSONString(JSONString)

        if let objectArray = mapArray(parsedJSON) {
            return objectArray
        }

        // failed to parse JSON into array form
        // try to parse it into a dictionary and then wrap it in an array
        if let object = map(parsedJSON) {
            return [object]
        }

        return nil
    }

    /// Maps a optional JSON String into an array of objects that conforms to Mappable
    public func mapArray(JSONString: String?) -> [N]? {
        if let JSONString = JSONString {
            return mapArray(JSONString)
        }

        return nil
    }

    /// Maps a JSON object to an array of Mappable objects if it is an array of JSON dictionary, or returns nil.
    public func mapArray(JSON: AnyObject?) -> [N]? {
        if let JSONArray = JSON as? [[String : AnyObject]] {
            return mapArray(JSONArray)
        }

        return nil
    }

    /// Maps an array of JSON dictionary to an array of Mappable objects
    public func mapArray(JSONArray: [[String : AnyObject]]) -> [N]? {
        // map every element in JSON array to type N
        let result = JSONArray.flatMap(map)
        return result
    }

    /// Maps a JSON object to a dictionary of Mappable objects if it is a JSON dictionary of dictionaries, or returns nil.
    public func mapDictionary(JSONString: String) -> [String : N]? {
        let parsedJSON: AnyObject? = Mapper.parseJSONString(JSONString)
        return mapDictionary(parsedJSON)
    }

    /// Maps a JSON object to a dictionary of Mappable objects if it is a JSON dictionary of dictionaries, or returns nil.
    public func mapDictionary(JSON: AnyObject?) -> [String : N]? {
        if let JSONDictionary = JSON as? [String : [String : AnyObject]] {
            return mapDictionary(JSONDictionary)
        }

        return nil
    }

    /// Maps a JSON dictionary of dictionaries to a dictionary of Mappble objects
    public func mapDictionary(JSONDictionary: [String : [String : AnyObject]]) -> [String : N]? {
        // map every value in dictionary to type N
        let result = JSONDictionary.filterMap(map)
        if result.isEmpty == false {
            return result
        }

        return nil
    }

    /// Maps a JSON object to a dictionary of Mappable objects if it is a JSON dictionary of dictionaries, or returns nil.
    public func mapDictionary(JSON: AnyObject?, toDictionary dictionary: [String : N]) -> [String : N] {
        if let JSONDictionary = JSON as? [String : [String : AnyObject]] {
            return mapDictionary(JSONDictionary, toDictionary: dictionary)
        }

        return dictionary
    }

    /// Maps a JSON dictionary of dictionaries to an existing dictionary of Mappble objects
    public func mapDictionary(JSONDictionary: [String : [String : AnyObject]], var toDictionary dictionary: [String : N]) -> [String : N] {
        for (key, value) in JSONDictionary {
            if let object = dictionary[key] {
                Mapper().map(value, toObject: object)
            } else {
                dictionary[key] = Mapper().map(value)
            }
        }

        return dictionary
    }

    /// Maps a JSON object to a dictionary of arrays of Mappable objects
    public func mapDictionaryOfArrays(JSON: AnyObject?) -> [String : [N]]? {
        if let JSONDictionary = JSON as? [String : [[String : AnyObject]]] {
            return mapDictionaryOfArrays(JSONDictionary)
        }

        return nil
    }

    ///Maps a JSON dictionary of arrays to a dictionary of arrays of Mappable objects
    public func mapDictionaryOfArrays(JSONDictionary: [String : [[String : AnyObject]]]) -> [String : [N]]? {
        // map every value in dictionary to type N
        let result = JSONDictionary.filterMap {
            mapArray([=16=])
        }

        if result.isEmpty == false {
            return result
        }

        return nil
    }

    /// Maps an 2 dimentional array of JSON dictionaries to a 2 dimentional array of Mappable objects
    public func mapArrayOfArrays(JSON: AnyObject?) -> [[N]]? {
        if let JSONArray = JSON as? [[[String : AnyObject]]] {
            var objectArray = [[N]]()
            for innerJSONArray in JSONArray {
                if let array = mapArray(innerJSONArray){
                    objectArray.append(array)
                }
            }

            if objectArray.isEmpty == false {
                return objectArray
            }
        }

        return nil
    }

    // MARK: Utility functions for converting strings to JSON objects

    /// Convert a JSON String into a Dictionary<String, AnyObject> using NSJSONSerialization
    public static func parseJSONDictionary(JSON: String) -> [String : AnyObject]? {
        let parsedJSON: AnyObject? = Mapper.parseJSONString(JSON)
        return Mapper.parseJSONDictionary(parsedJSON)
    }

    /// Convert a JSON Object into a Dictionary<String, AnyObject> using NSJSONSerialization
    public static func parseJSONDictionary(JSON: AnyObject?) -> [String : AnyObject]? {
        if let JSONDict = JSON as? [String : AnyObject] {
            return JSONDict
        }

        return nil
    }

    /// Convert a JSON String into an Object using NSJSONSerialization
    public static func parseJSONString(JSON: String) -> AnyObject? {
        let data = JSON.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
        if let data = data {
            let parsedJSON: AnyObject?
            do {
                parsedJSON = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
            } catch let error {
                print(error)
                parsedJSON = nil
            }
            return parsedJSON
        }

        return nil
    }
}

extension Mapper {

    // MARK: Functions that create JSON from objects    

    ///Maps an object that conforms to Mappable to a JSON dictionary <String : AnyObject>
    public func toJSON(var object: N) -> [String : AnyObject] {
        let map = Map(mappingType: .ToJSON, JSONDictionary: [:])
        object.mapping(map)
        return map.JSONDictionary
    }

    ///Maps an array of Objects to an array of JSON dictionaries [[String : AnyObject]]
    public func toJSONArray(array: [N]) -> [[String : AnyObject]] {
        return array.map {
            // convert every element in array to JSON dictionary equivalent
            self.toJSON([=16=])
        }
    }

    ///Maps a dictionary of Objects that conform to Mappable to a JSON dictionary of dictionaries.
    public func toJSONDictionary(dictionary: [String : N]) -> [String : [String : AnyObject]] {
        return dictionary.map { k, v in
            // convert every value in dictionary to its JSON dictionary equivalent
            return (k, self.toJSON(v))
        }
    }

    ///Maps a dictionary of Objects that conform to Mappable to a JSON dictionary of dictionaries.
    public func toJSONDictionaryOfArrays(dictionary: [String : [N]]) -> [String : [[String : AnyObject]]] {
        return dictionary.map { k, v in
            // convert every value (array) in dictionary to its JSON dictionary equivalent
            return (k, self.toJSONArray(v))
        }
    }

    /// Maps an Object to a JSON string with option of pretty formatting
    public func toJSONString(object: N, prettyPrint: Bool = false) -> String? {
        let JSONDict = toJSON(object)

        return Mapper.toJSONString(JSONDict, prettyPrint: prettyPrint)
    }

    /// Maps an array of Objects to a JSON string with option of pretty formatting  
    public func toJSONString(array: [N], prettyPrint: Bool = false) -> String? {
        let JSONDict = toJSONArray(array)

        return Mapper.toJSONString(JSONDict, prettyPrint: prettyPrint)
    }

    public static func toJSONString(JSONObject: AnyObject, prettyPrint: Bool) -> String? {
        if NSJSONSerialization.isValidJSONObject(JSONObject) {
            let JSONData: NSData?
            do {
                let options: NSJSONWritingOptions = prettyPrint ? .PrettyPrinted : []
                JSONData = try NSJSONSerialization.dataWithJSONObject(JSONObject, options: options)
            } catch let error {
                print(error)
                JSONData = nil
            }

            if let JSON = JSONData {
                return String(data: JSON, encoding: NSUTF8StringEncoding)
            }
        }
        return nil
    }
}

extension Mapper where N: Hashable {

    /// Maps a JSON array to an object that conforms to Mappable
    public func mapSet(JSONString: String) -> Set<N>? {
        let parsedJSON: AnyObject? = Mapper.parseJSONString(JSONString)

        if let objectArray = mapArray(parsedJSON){
            return Set(objectArray)
        }

        // failed to parse JSON into array form
        // try to parse it into a dictionary and then wrap it in an array
        if let object = map(parsedJSON) {
            return Set([object])
        }

        return nil
    }

    /// Maps a JSON object to an Set of Mappable objects if it is an array of JSON dictionary, or returns nil.
    public func mapSet(JSON: AnyObject?) -> Set<N>? {
        if let JSONArray = JSON as? [[String : AnyObject]] {
            return mapSet(JSONArray)
        }

        return nil
    }

    /// Maps an Set of JSON dictionary to an array of Mappable objects
    public func mapSet(JSONArray: [[String : AnyObject]]) -> Set<N> {
        // map every element in JSON array to type N
        return Set(JSONArray.flatMap(map))
    }

    ///Maps a Set of Objects to a Set of JSON dictionaries [[String : AnyObject]]
    public func toJSONSet(set: Set<N>) -> [[String : AnyObject]] {
        return set.map {
            // convert every element in set to JSON dictionary equivalent
            self.toJSON([=16=])
        }
    }

    /// Maps a set of Objects to a JSON string with option of pretty formatting
    public func toJSONString(set: Set<N>, prettyPrint: Bool = false) -> String? {
        let JSONDict = toJSONSet(set)

        return Mapper.toJSONString(JSONDict, prettyPrint: prettyPrint)
    }
}

extension Dictionary {
    internal func map<K: Hashable, V>(@noescape f: Element -> (K, V)) -> [K : V] {
        var mapped = [K : V]()

        for element in self {
            let newElement = f(element)
            mapped[newElement.0] = newElement.1
        }

        return mapped
    }

    internal func map<K: Hashable, V>(@noescape f: Element -> (K, [V])) -> [K : [V]] {
        var mapped = [K : [V]]()

        for element in self {
            let newElement = f(element)
            mapped[newElement.0] = newElement.1
        }

        return mapped
    }


    internal func filterMap<U>(@noescape f: Value -> U?) -> [Key : U] {
        var mapped = [Key : U]()

        for (key, value) in self {
            if let newValue = f(value){
                mapped[key] = newValue
            }
        }

        return mapped
    }
}

您已定义:

var selectedHomes              : [MyHomes]?

然后你尝试:

self.selectedHomes?.append(datasinhomes)

但我在任何地方都看不到您实际将数组分配给 selectedHomes 以便实际定义其值。尝试附加到 nil 显然不会做任何事情。

尝试将您的定义更改为:

var selectedHomes : [MyHomes]? = []

如果您想将其保留为可选数组,或者此时也可以将其设为非可选数组:

var selectedHomes : [MyHomes] = []

祝你好运!