user1594257
user1594257

Reputation: 495

Access structure in Swift array

Can I access a struct inside of an array that is defined in the application delegate from a ViewController?

I get the error: 'Any' does not have a member named 'title' in XCode 6.2

What is the syntax for accessing a structure inside of the array?

//AppDelegate.swift
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

struct TodoItem {
    var title: String
}

var todoItem = TodoItem(
    title: "Get Milk")

var myArray: [Any] = []

And then in the ViewController

//
//  ViewController.swift


import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let delegate = UIApplication.sharedApplication().delegate as AppDelegate

    //here I'm adding the struct to the array
    let myTodoItem = delegate.myArray.append(delegate.todoItem)

    //how do I access the items in the struct?
    println(delegate.myArray[0].title)

Upvotes: 2

Views: 2431

Answers (5)

Shoaib
Shoaib

Reputation: 2294

Solution1: Your struct is defined under AppDelegate class so, you will have to parse it like this;

    //** Swift 1.2, xCode 6.3.1**//
    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate

    //here I'm adding the struct to the array
    delegate.myArray.append(delegate.todoItem)

    //how do I access the items in the struct?
    println((delegate.myArray[0] as! AppDelegate.TodoItem).title)

Solution2: Change the data type of your array Any to TodoItem

var myArray: [TodoItem] = []

and then, it would work;

    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate

    //here I'm adding the struct to the array
    delegate.myArray.append(delegate.todoItem)

    //how do I access the items in the struct?
    println(delegate.myArray[0].title)

Upvotes: 1

Daij-Djan
Daij-Djan

Reputation: 50139

You can access structures in array same as you would classes.

Your issue is that you explicitly tell that the array contains Any Objects. Type the array to be of type MyStruct and it works fine:

var myArray: [MyStructure] = [];

alternatively, if you can't modify the array declaration, cast:

myValueIWannaRead = (myArray[0] as MyStruct).myValueIWannaRead

Upvotes: 3

luizParreira
luizParreira

Reputation: 1107

Why do you have to add the struct TodoItem to AppDelegate? in my point of view, it would be better to create it in the same file that you have your view controller or - even better - create a new Swift file named TodoItem.swift holding the struct.

Then after you have moved your struct to a new file or inside your View Controller file, but outside your ViewController class. You can just call:

let myTodoItem = TodoItem(title: "Get Milk") // declaring TodoItem
var myTodoItemArray : [TodoItem] = [] // declaring TodoItem array
myTodoItemArray.append(myTodoItem) // Appending to the array

// then you can set it by calling the array only member
let todoItem = myTodoItemArray[0] as! TodoItem
let title = todoItem.title

// Or you can just call the toDo Item itself
let title = myTodoItem.title

If you want to comunicate this data between two different classes, I would suggest use Delegation by creating a protocol or Notifications with NSNotifications.

I hopw this helps, happy coding.

EDIT: Fixed some minor error in the code

Upvotes: 1

vadian
vadian

Reputation: 285290

In Swift be always as specific as possible.

If the array contains only items of type TodoItem, declare it respectively.

var myArray: [TodoItem] = []

Any is a kind of placeholder, the compiler has no idea what dynamic type it is.

Upvotes: 1

giorashc
giorashc

Reputation: 13713

You can define the array to store the type you desire :

var myArray: [TodoItem] = [];

Then you can access the instances properly.

The Any type denotes an object of an unknown type (class/primitive type ) and during compilation time there is no way to know which type the instance accessed will be of.

Upvotes: 1

Related Questions