简体   繁体   中英

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)

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

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.

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM