6 Replies - 545 Views - Last Post: 03 June 2017 - 09:50 AM Rate Topic: -----

#1 spmedia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-June 17

Swift code error

Posted 02 June 2017 - 12:51 PM

Hi, I'm only getting started with swift coding and have stumbled across my first problem i cannot fix myself... Does anyone know what i have done wrong?


Kind regards
Aaron
Is This A Good Question/Topic? 0
  • +

Replies To: Swift code error

#2 spmedia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-June 17

Re: Swift code error

Posted 02 June 2017 - 12:58 PM

 truct edsheeranlibrary
{
    
    //title : string
    //description : some history - String
    //CoverImageName : String
    //Songs [string]
    
    //Each album must be a dictionary
    
    let album: [String : AnyObject]
    "title" : "castle on the hill",
    "description" : "Realesed in october 2016 ed sheeran is the best by reaching top off the chatrs",
    "coverImageName" : "castle on the hill",
    "songs" : ["castle on the hill"]
    ]
    
    //an array of dictionairies. each dictionairy is an album.
    let albums: [[String: AnyObject]] = [
        "title" : "castle on the hill",
        "description" : "Realesed in october 2016 ed sheeran is the best by reaching top off the chatrs",
        "coverImageName" : "Galway Girl",
        "songs" : ["castle on the hill", "galway girl"]
]

}


Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,864
  • Joined: 12-June 08

Re: Swift code error

Posted 02 June 2017 - 01:03 PM

Please explain any errors you are getting.
Was This Post Helpful? 0
  • +
  • -

#4 spmedia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-June 17

Re: Swift code error

Posted 02 June 2017 - 01:07 PM

View Postmodi123_1, on 02 June 2017 - 08:03 PM, said:

Please explain any errors you are getting.

Hi, here is a screenshot off the errors. http://imgur.com/dqIf42t
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,983
  • Joined: 21-June 11

Re: Swift code error

Posted 02 June 2017 - 02:03 PM

Compare line 11 to line 19. On line 19 you start a dictionary literal, so the following lines are part of that dictionary. On line 11 you don't, so the following lines are seen as declarations of their own, which leads to a syntax error because "title" : "castle on the hill", etc. are not valid declarations.
Was This Post Helpful? 0
  • +
  • -

#6 spmedia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-June 17

Re: Swift code error

Posted 03 June 2017 - 12:09 AM

View Postsepp2k, on 02 June 2017 - 09:03 PM, said:

Compare line 11 to line 19. On line 19 you start a dictionary literal, so the following lines are part of that dictionary. On line 11 you don't, so the following lines are seen as declarations of their own, which leads to a syntax error because "title" : "castle on the hill", etc. are not valid declarations.


Hi, I fixed a few errors however I'm getting more that I don't understand fully yet. Thanks for your help :) here is another screen shot off the final errors. https://i.imgur.com/ue4u9X8.png
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,983
  • Joined: 21-June 11

Re: Swift code error

Posted 03 June 2017 - 09:50 AM

According to Google, String is a value type in Swift and can't be stored as AnyObject. You'll either want Any instead or cast the string to NSString, which is a reference type.

Your second error is that you're declaring albums as an array of dictionaries, but then try to assign it a dictionary, not an array.

PS: Representing albums as a dictionary with Any (or AnyObject) values strikes me as inherently type unsafe. Why not use a struct or class instead?

This post has been edited by sepp2k: 03 June 2017 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1