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
23
votes
5 answers

swift 3 - ios : convert anyObject to string

How could we convert anyobject to string in swift 3, it's very easy in the older version by using. var str = toString(AnyObject) I tried String(AnyObject) but the output is always optional, even when i'm sure that AnyObject is not a optional value.
Anthony Shahine
  • 2,477
  • 3
  • 18
  • 24
23
votes
1 answer

Class-only generic constraints in Swift

I'm trying to mark a variable of a generic type as weak: class X { weak var t: T? } If I don't put in any constraints for T I get the error weak cannot be applied to non-class type 'T'. If I would only use use this with NSObject derived…
lassej
  • 6,256
  • 6
  • 26
  • 34
19
votes
3 answers

type any? has no subscript members

I want to get Addresses from profile dictionary,but I got the error "type any? has no subscript members" var address:[[String : Any]] = [["Address": "someLocation", "City": "ABC","Zip" : 123],["Address": "someLocation", "City": "DEF","Zip" :…
KKG
  • 299
  • 1
  • 4
  • 11
16
votes
2 answers

Swift AnyObject's subscript, where did it come from?

In Swift, how is it that AnyObject supports subscripts, even for types that are't subscriptable? Example: let numbers: AnyObject = [11, 22, 33] numbers[0] // returns 11 let prices: AnyObject = ["Bread": 3.49, "Pencil": 0.5] prices["Bread"] …
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
12
votes
3 answers

Casting from AnyObject to CGColor? without errors or warnings

Hello StackOverflow :) I've been running into a weird problem since I upgraded to swift 2.0 I'm trying to set a border color, so I'm writing self.layer.borderColor = borderColor as! CGColor where borderColor is an AnyObject, while…
Jacob R
  • 1,243
  • 1
  • 16
  • 23
10
votes
4 answers

How Does AnyObject Conform to NSObjectProtocol?

This question was inspired by mz2's answer on the question Check for object type fails with "is not a type" error. Consider an empty Swift class: class MyClass { } Attempting to call any NSObjectProtocol methods on an instance of this class will…
JAL
  • 41,701
  • 23
  • 172
  • 300
9
votes
2 answers

Non-optional expression of type 'AnyObject' used in a check for optionals

I created an extension on 'Dictionary' to help me parse JSON. The method below helps me do this: func toJSONString() -> String? { if let dict = self as? AnyObject { if let data = try? JSONSerialization.data(withJSONObject: dict, options:…
Faisal Syed
  • 921
  • 1
  • 11
  • 25
8
votes
1 answer

Swift Generics vs Any

I read swift documentation in apple site. There is a function swapTwoValues, which swaps two any given values func swapTwoValues1(_ a: inout T, _ b: inout T) { let temporaryA = a a = b b = temporaryA } Now I want to write similar…
Karen Karapetyan
  • 704
  • 1
  • 9
  • 18
8
votes
4 answers

AnyObject vs. Struct (Any)

I would like to create a method like this for my projects: func print(obj: AnyObject) { if let rect = obj as? CGRect { println(NSStringFromCGRect(rect)) } else if let size = obj as? CGSize { …
Paulo Cesar
  • 2,250
  • 1
  • 25
  • 35
7
votes
2 answers

Why Swift doesn't type inference to Any when put multiple type item in Array

there are two situation make me confuse when develop swift 2.2 by using Xcode 7.1, please see the example below, thanks First, when import Foundation, I declared an testArray which contains two item, an Integer type 1 and a String type "hello", my…
c41ux
  • 73
  • 4
6
votes
3 answers

Protocol of reference type to generic constraint of type AnyObject

I have a generic struct declared as follows: struct WeakReference { weak var value: T? init(value: T?) { self.value = value } } And a protocol: protocol SomeProtocol: class { } But I'm not able to declare a…
Shubham
  • 935
  • 1
  • 7
  • 25
6
votes
2 answers

Swift Generics equivalent of Java any type

In Java you can some times use generics without caring about the actual type. Can you do that in Swift? For instance MyClass doesn't work like MyClass would in Java. At I'd expect it to work the same. Is there any other way?
Cosmin
  • 2,840
  • 5
  • 32
  • 50
5
votes
1 answer

swift 3 array of structs -> cast to NSObject -> cast back => crash

The following code crashes on Swift 3, can anyone please explain why? struct S { let a:Int } let t = [S(a: 8)] let u:AnyObject = t as NSObject let v:[S] = u as! [S] Is that because in Swift 3 array of structs is NSObject (it's not in Swift 2)…
silyevsk
  • 4,021
  • 3
  • 31
  • 30
4
votes
1 answer

Class-only protocol as typealias for associatedtype with AnyObject constraints

In Swift 4.0 I could write something like this protocol ObserversHolder { ///Compiling Error in Swift 4.1 ///note: possibly intended match 'StringManager.ObserverValue' (aka 'StringObserver') does not conform to 'AnyObject' ///note:…
Vladislav
  • 73
  • 6
4
votes
1 answer

What is the difference between [String: AnyObject] and [String: Any]?

I usually use [String: AnyObject]. But I noticed that Alamofire uses [String: Any]. I read somewhere that Any is "superior" than AnyObject or "encompasses" AnyObject. But other than that, I don't know if there's any different between them. Is there…
Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124
1
2 3
8 9