Swift Array - use "Contains" of type AnyObject

Swift Array - use "Contains" of type AnyObject

Content Index :

Swift Array - use "Contains" of type AnyObject
Tag : ios , By : barefootChild
Date : November 24 2020, 01:01 AM

hope this fix your issue I agree with the comments and other answer; AnyObject is not good practice, but if you really want to use AnyObject, you can treat your array of AnyObjects as an NSArray object and then use the function containsObject():
if (resultArray as NSArray).containsObject(AnyObjectOfAnyType) {
     // Do something

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Swift arrays identity "Type does not conform to protocol 'AnyObject'" error

Tag : arrays , By : Priya
Date : March 29 2020, 07:55 AM
it helps some times I create playground and write this code: , According to The Swift Programing Language:
var a = [1, 2, 3] as NSArray //NSArray is not a Struct
var b = a
a === b //true
var a = [1, 2, 3]
var b = a
a == b //true

Swift Array: [AnyObject] vs. [xxxxxxxxClass] and the method "append"

Tag : arrays , By : David B
Date : March 29 2020, 07:55 AM
it should still fix some issue Your original version works for me (screenshot) (only slightly modified for testing with my data). You shouldn't have to do this dance, something else is causing trouble.
I suggest cleaning up your class a bit and take advantage of Swift 2 using guard, map and error. It will be easier to debug and will work more efficiently anyway.
class ProductData: NSObject {
    var title = ""

    private init(dict: [String : AnyObject]){
        if let t = dict["title"] as? String { self.title = t }

    class func getTheData(fromJSONPath JSONPath: String) -> [ProductData] {

        do {
            // safely unwrap and typecast the values else return empty array
            guard let url = NSURL(string: JSONPath),
                let JSONData = NSData(contentsOfURL: url),
                let JSONArray = try NSJSONSerialization.JSONObjectWithData(JSONData, options: [])
                    as? [[String : AnyObject]] else { return [] }

            return JSONArray.map() { ProductData(dict: $0) }

        } catch {
            // this `error` variable is created by the `catch` mechanism
            // return empty array if unkown failure
            return []



let test = ProductData.getTheData(fromJSONPath: "http://localhost:5678/file/test.json")

iOS SWIFT - File Archive - Argument Type "[String?]" : does not conform to expected type'AnyObject'

Tag : ios , By : Jason Jennings
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am trying to write into an Archive and getting this error . , Array is not of type AnyObject
You should try
NSKeyedArchiver.archiveRootObject(NSArray(array: contactArray),
    toFile: dataFilePath!)
var contactArray = [name.text!, address.text!, phone.text!]
var contactArray = [String]()

for element in [name.text, address.text, phone.text] where element != nil {
NSKeyedArchiver.archiveRootObject(NSArray(array: contactArray),
    toFile: dataFilePath!)

"Cannot assign value of type 'String' to type 'AnyObject?'", Swift 3, Xcode 8 beta 6

Tag : swift , By : inquiringmind
Date : March 29 2020, 07:55 AM
wish helps you In b6, String no longer magically bridges to NSString. String is not a class; it's a struct. You need to do the bridging by hand:
dict["key"] = "value" as AnyObject

Type "Any" has no subscript members despite casting as AnyObject on Swift 3?

Tag : swift , By : user130518
Date : March 29 2020, 07:55 AM
should help you out It's because you're using more than one subscript operator, because presumably this is something like an array of arrays. But NSMutableArray's subscript operator returns Any. As a result, cellDescriptors[0] is Any. You try to use [11] on the result, but Any doesn't accept subscripts because it's Any, not a collection type.
Casting to AnyObject doesn't help because AnyObject is also not a collection type.
Related Posts Related QUESTIONS :
  • How do you initialize/use UITableViewCells with CellStyle = .value1 programmatically?
  • How to know if Apple has received our appeal/how long does it take Apple to respond?
  • Select new item added to SwiftUI List
  • What is the correct length: argument to provide to NSRange for NSRegularExpression using a (Swift) String?
  • I am trying to delete data from database, but sometime data is deleted and sometime data is not deleted
  • How to get slider value with subview rotation angle
  • Can't pass custom UINavigationBar / UIToolbar to UINavigationController because they don't conform to AnyClass
  • Grab SELECTED text on UITextView
  • Clever ways to better test GPS code using only the iPhone simulator?
  • Remove app before simulator or device is lunched from Xcode
  • Auto Layout constraints for container view inside UITableView header broken for different devices
  • iPhone restarts after API call
  • What is the shortcut / How do I create function after writing it's usage in Xcode
  • Convert apple time stamp to Unix time
  • No podspec found for `React-fishhook`
  • How to test touch dialog and perform real in-app purchase?
  • Compiling Metal shaders from source on iOS 13 Simulator gives PCH error
  • conan error:I need to cross build packages for os:iOS with my Macos, but when the first package was done, it broke my cp
  • Update calendar 'start of week' from phones system calendar
  • Xcode is showing some warnings
  • How to remove the default Navigation Bar space in SwiftUI NavigiationView
  • Having trouble integrating multiple pickerviews in the same view
  • SCNNode not in SCNView hierarchy, still showing & getting touches & physics
  • How can I open my SwiftUI app and perform a function using NSUseractivity from within Siri Intent Extension?
  • NSCoding decodeObject always nil
  • Passing data in a segmented Controller?
  • Facing issue in Siri Integration with custom intents
  • ARKit3 Hardware Requirement Clarification
  • ARKit3 How to use TrueDepth camera for face tracking and face meshes of other people?
  • How to discriminate while decoding using Codable Protocol in Swift?
  • 'IPA processing failed' while distributing build in Xcode Beta
  • How to debug a crash which happened when come back from background
  • Synchronising old data with NSPersistentCloudKitContainer
  • iOS app opened from web app with url redirects to Appstore
  • How to change the color of seperator in tableView?
  • Is there a better way to watch for a key-value change?
  • How to make UIBezierPath to fill from the center
  • Why on Xcode 11, UICollectionViewCell changes size as soon as you scroll (I already set size in sizeForItem AtIndexPath:
  • How to scan non LE devices using Swift?
  • Unable to infer closure type in the current context in SwiftUI
  • What is the best way to write common code and use it entire project
  • UISegmentedControl inside UIStackView, segments text not centered - iOS 13
  • Swift & ObjC bridge - cannot find protocol declaration for "WKNavigationDelegate"
  • SwiftUI How to push to next screen when tapping on Button
  • 'NSApp' is unavailable in UIKit for Mac: How to embed Mac only framework in iOS app on macOS?
  • UserDefaults Binding with Toggle in SwiftUI
  • How to create a color with an alpha value using SwiftUI?
  • SwiftUI creates destination views before the user navigates to them
  • Streaming video from https with AVPlayer causes initial delay
  • Tap Action not working when Color is clear SwiftUI
  • SwiftUI NavigationButton: How to navigate to different destinations
  • Supported default light/dark mode colors for Color vs UIColor
  • XCUITest wait for a cell with a specific static text inside it
  • Not able to enable Development mode on iOS 13
  • How to hide the home indicator with SwiftUI?
  • How to write if condition with using label text (Int) values
  • Swift Cannot use instance member within property initializer
  • Issue while getting the frame of the keyboard
  • dynamicMemberLoookup ExpressibleByStringLiteral parameter swift
  • iOS Unity Firebase Messenger Xcode Compile Error: FIRDynamicLinks
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com