Questions tagged [anyobject]

`AnyObject` can be used to represent any type in Swift that is a class. Value types (Structs and enums) can be represented using `Any`. Use this tag for questions concerning the use of `AnyObject` and how to handle it.

AnyObject can be used to represent any type in Swift that is a class. Value types (Structs and enums) can be represented using Any. In migration from Objective-C to Swift, some methods might return a reference type in Objective-C whose type cannot be precisely known. This is where it's type is represented as AnyObject in Swift.

121 questions
0
votes
1 answer

Generic XCTAssertEqual with AnyObject arguments?

When testing NSLayoutConstraint items, this line doesn't compile: XCTAssertEqual(firstItem, view1) Error: Cannot invoke 'XCTAssertEqual' with an argument list of type '(AnyObject, AnyObject)' To get it to compile without errors or warnings, I…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
0
votes
1 answer

Array cannot be bridged from Objective-C error

After instantiating an array as an AnyObject type and adding string objects to the array the following code compiles however crashes when attempting to display this array in a textfield. The program still crashes when attempting to print it out to…
user5369493
0
votes
0 answers

Global Variable Not Working in Swift

I have created a small tabbed view program that uses tabs to switch between two views. There is one global variable called list that is an array containing information that both views need to access and change. However, when I try to use…
0
votes
3 answers

Swift 2 Dictionary to Struct getter/setter throwing Cannot subscript value ... with an index of type 'String'

I'm writing a getter/setter for a class property that uses a struct. This is ultimately parsed from JSON and converted back to JSON in order to store data in the cloud. // struct: public struct CompletedTask { var uuid: String! var amount:…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
0
votes
1 answer

Filter AnyObject in Swift 2

I have an JsonArray named data which I pass to AnyObject: if let dtMenu: AnyObject = responseObject?.valueForKey("data") { print(filteredMenu) } // I got JsonArray here // My data are "data":[ { …
Syed Md. Kamruzzaman
  • 979
  • 1
  • 12
  • 33
0
votes
1 answer

Converting AnyObject? to NSDate

I am trying to fetch scores (NSDates) from core data and return a date. Returning 00:00:00.00 if the coreData is empty and displaying the first date if it isn't. I have the following code: func getHighScore() -> NSDate { let appDel:AppDelegate =…
Jess Murray
  • 1,273
  • 1
  • 10
  • 32
-1
votes
2 answers

Swift: Thread 1: signal SIGABRT ( get child of AnyObject )

My code: let userData = result["user"] as? AnyObject // { test="x"; id=0; phone="none" } var id = Int(userData?["id"] as? String ?? String(userData?["id"] as? String ?? "0"))! After compile i get this error : 2018-07-16 18:21:02.747944+0430…
Computer Im
  • 85
  • 1
  • 11
-1
votes
1 answer

Do all class types in Swift implicitly implement AnyObject?

There is nothing in the Swift documentation that specifically mentions this other than AnyObject can represent an instance of any class type. Do all class types in Swift implicitly implement AnyObject protocol? class C {} // Is this implicitly…
Boon
  • 40,656
  • 60
  • 209
  • 315
-1
votes
1 answer

AnyObject & Cannot assign to immutable expression of type 'CGFloat'

I have two example functions. Both do exactly the same thing. The difference lies in the type of the variable "objectName". The first works. I am specific and set the type to UILabel. Obviously I can only use it with UILabel type objects and…
-1
votes
1 answer

Swift check if array of anyObject have value

I have this array of anyObject that i use to read my JSON data downloaded. datiArticolo!["anagrafica"]![linguaApp!]!!["img"]!!["path"] The problem is that not always i have che ["img"] or/and ["path"] values. how can i check if there are and if not…
WillyWon
  • 15
  • 3
-1
votes
2 answers

converting AnyObject to String in swift not working

I make POST request to the server from my app, and I get jsonString as the response. I have made function to convert string to dictionary which looks like this: func convertStringToDictionary(text: String) -> [String:AnyObject]? { if let…
-2
votes
1 answer

Value of type 'AnyObject' has no member 'hashValue'

i am using this LocationPicker in my project and when i build the project the library has an error saying this: Value of type 'AnyObject' has no member 'hashValue' so i opened the code and i found the error line here the code: open override func…
Jhon
  • 99
  • 3
  • 15
-2
votes
2 answers

Swift AnyObject as Dictionary get element that doesn't exist is not nil

I'm confronted to a problem with cast in Swift Here the code : init(response: NSHTTPURLResponse, representation: AnyObject) { super.init(entity:NSEntityDescription.entityForName("File", inManagedObjectContext:…
Fogia
  • 127
  • 1
  • 8
-3
votes
1 answer

Sort array of objects into ascending order

I have an array of objects. Each object has a key "username". Is there a swift way of sorting this array of objects into ascending order? (array(at: indexPath.row) as AnyObject).value(forKey: "username") as? String)! Thanks
user2512523
  • 1,229
  • 2
  • 15
  • 25
-3
votes
1 answer

Android - AnyObject for Android like iOS Swift 2

I need help, This is example for AnyObject Class in Swift. class OtherClass { private var myValue: String? func setValue(myValue: String) { self.myValue = myValue } func getValue() -> String { return myValue! } } class…
icaksama
  • 712
  • 10
  • 15
1 2 3
8
9