logo
down
shadow

Filter Array of [AnyObject] in Swift


Filter Array of [AnyObject] in Swift

By : Nollaig MacNioclais
Date : November 22 2020, 01:01 AM
may help you . Your array, objects, is an array of PFObject objects. Thus, to filter the array, you might do something like:
code :
let filteredArray = objects.filter() {
    if let type = ($0 as PFObject)["Type"] as String {
        return type.rangeOfString("Sushi") != nil
    } else {
        return false
    }
}
class Restaurant {
    var city: String
    var name: String
    var country: String
    var type: [String]!

    init (city: String, name: String, country: String, type: [String]!) {
        ...
    }
}
let filteredArray = objects.filter() {contains(($0 as Restaurant).type, "Sushi")}
let filteredArray = objects.filter() {
    if let type = ($0 as Restaurant).type as [String]! {
        return contains(type, "Sushi")
    } else {
        return false
    }
}


Share : facebook icon twitter icon
Filter a Parse query with an Array of [AnyObject] in Swift

Filter a Parse query with an Array of [AnyObject] in Swift


By : user3369451
Date : March 29 2020, 07:55 AM
it should still fix some issue Here's a simple example that returns an array of matches in all fields:
code :
func filterContentForSearchTextInAllFields(searchText: String) -> [String] {
    var results = [String]()
    for publication in publications {
        for (key, value) in publication {
            if (value as NSString).containsString(searchText) {
                results.append(value)
            }
        }
    }
    return results
}

println(filterContentForSearchTextInAllFields("blog"))
func filterContentForSearchText(searchText: String) -> [String] {
    var results = [String]()
    for publication in publications {
        if let fullTitle = publication["fullTitle"] {
            if (fullTitle as NSString).containsString(searchText) {
                results.append(fullTitle)
            }
        }
    }
    return results
}

println(filterContentForSearchText("first"))
func filterContentForSearchText(searchText: String) -> [[String:String]] {
    var results = [[String:String]]()
    for publication in publications {
        if let fullTitle = publication["fullTitle"] as? String {
            if (fullTitle as NSString).containsString(searchText) {
                results.append(publication as! [String : String])
            }
        }
    }
    return results
}

println(filterContentForSearchText("first"))
Filter AnyObject in Swift 2

Filter AnyObject in Swift 2


By : geofrey boniphace
Date : March 29 2020, 07:55 AM
This might help you I have an JsonArray named data which I pass to AnyObject: , Try this.
code :
    var predicate = NSPredicate(format: "%K == %@", "MENUITEMTYPE", "R")

    let filteredArray = yourArray.filter { predicate.evaluateWithObject($0) };
Siesta Swift Casting from ImplicitlyUnwrappedOptional<Swift.AnyObject> to Array<AnyObject>

Siesta Swift Casting from ImplicitlyUnwrappedOptional<Swift.AnyObject> to Array<AnyObject>


By : Eman Anwar
Date : March 29 2020, 07:55 AM
help you fix your problem I am configuring ResponseTransformer of siesta to return array of objects. , You need to map your response, like this
code :
configureTransformer("/models/*") {
    ($0.content).map(Model.instantiate)
}
let objects = resource.typedContent() ?? []
Swift AnyObject - Down casting an array of protocols to [AnyObject]

Swift AnyObject - Down casting an array of protocols to [AnyObject]


By : Noor Ul Ain
Date : March 29 2020, 07:55 AM
Hope this helps It's less than perfect, but it works if the protocol and class are both @objc (and the class subclasses NSObject):
code :
@objc protocol Nameable: class {
    var name: String { get }
}

@objc class Person: NSObject, Nameable {
    var name: String
    init(name: String) {
        self.name = name
    }
}
var array: [Nameable] = [personOne, personTwo]
let array2 = array as [AnyObject] // ✓
How to remove dictionary[String:AnyObject] for array using filter function in swift?

How to remove dictionary[String:AnyObject] for array using filter function in swift?


By : Lisa
Date : March 29 2020, 07:55 AM
Hope this helps , Try using this
code :
    var globalCountArray = [AnyObject]()
    var assetDictionary = [String:AnyObject]()
    globalCountArray.append(assetDictionary as AnyObject)
    let dict = [String:AnyObject]()




    globalCountArray = globalCountArray.filter({ (obj) -> Bool in

        if obj is[String:AnyObject] {

            return (obj as! [String:AnyObject]) != dict 


        }
        return false
    })
globalCountArray = globalCountArray.filter({ (obj) -> Bool in

            if obj is[String:AnyObject] {

                return (obj as! [String:AnyObject]) == dict 


            }
            return true
        })
public func !=(lhs: [String: AnyObject], rhs: [String: AnyObject] ) -> Bool {
    return !NSDictionary(dictionary: lhs).isEqual(to: rhs)
}

 public func ==(lhs: [String: AnyObject], rhs: [String: AnyObject] ) -> Bool {
    return NSDictionary(dictionary: lhs).isEqual(to: rhs)
}
Related Posts Related Posts :
  • Swift/iOS: How to access Object properties when type-casted from Protocol?
  • consecutive statement need to have ; error in swift
  • Trying to create a 32 bpc NSBitmapImageRep, getting hit with errors
  • Does locationManager:didUpdateLocations: method called at the same time in extension and its containing app
  • Can't get Data using SwiftyJSON
  • Convert String to UnsafeMutablePointer<char_t> in Swift
  • inout param with empty object since nil won't work
  • How to align a mask onto an image
  • compiler forcing self inside static method of a function while using dispatch_sync
  • private static variable in struct
  • Conditional "in" for arrays in Swift
  • Extension for Double to accept String as initializer in Swift
  • Modifying UIButton Programmatically
  • Pass type to generic function and compare
  • How to customise the look of the PFSignUpViewController
  • How to take screen shot programmatically (Swift, SpriteKit)
  • Setting NavigationController's NavigationBar translucent property to false causes extra padding
  • Create multiple UIViews when the user taps the screen
  • How to add ParseUI to a swift project
  • Can Swift enums have multiple raw values?
  • Using a function() variable in a different Class - Swift
  • Masking an image in Swift using CALayer and UIImage
  • KVC without NSObject (NSPredicate)
  • Any? to Dictionary Cast and retrieving value
  • NSCharacterSet.characterIsMember() with Swift's Character type
  • SCNScene: Calculate projected size of an object
  • How to use contains on a swiftyJSON object
  • Index of element in sorted()
  • How do I initialise a new NSDocument instance in Swift?
  • Multiple functions with the same name
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org