0

I'm trying to initialise a value in dictionary as follow,

var og:image: String

But after og: it tries to assign the type considering og as the variable to site_name, which is obviously wrong.

Is there a way to assign og:image as variable to String type using special or escape characters?

In reference to this, apple does not provide any meaningful explanations to variable naming conventions in such a situation.

Edit-1:

Here is code snippet to clarify dictionary usage is in JSON parsing data structure,

struct Metadata: Decodable{
    var metatags : [enclosedTags]
}
struct enclosedTags: Decodable{
    var image: String
    var title: String
    var description: String
    var og:site_name: String
}
Frostmourne
  • 156
  • 1
  • 19
  • cant use in declaration but you can append in the string at a later stage as value. – Gagan_iOS Mar 14 '18 at 07:48
  • Which *dictionary* are you talking of? – And the valid identifier characters are documented at https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-ID412, `:` is not among them. – Martin R Mar 14 '18 at 07:54
  • @MartinR I have edited to shed some light on the dictionary part. – Frostmourne Mar 14 '18 at 08:06
  • 3
    @HamzaIqbal You want to use a custom `CodingKeys` enum for that, compare https://stackoverflow.com/q/44396500/2976878 – Hamish Mar 14 '18 at 08:10
  • 2
    Your question is a good example for an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) :) – The above link from Hamish solves your *real* problem. – Martin R Mar 14 '18 at 08:17
  • Rightly as pointed, I have corrected question title to clarify the problem@MartinR – Frostmourne Mar 15 '18 at 06:50

3 Answers3

1

You cannot use : (colon). But if you really want:

var ogCOLONimage: String

Joking apart. You could use a Dictionary or something like that:

var images: [String: String] = ["og:image" : "your string"]

Now you can access your og:image data with images["og:image"].

a.u.b
  • 1,589
  • 10
  • 25
0

Swift allow you to use almost any character when naming variables. You can even use Unicode characters.

However, there are a few restrictions:

Constant and variable names can’t contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters. Nor can they begin with a number, although numbers may be included elsewhere within the name.

Said that, it is not possible to use : in the name of a variable. However, you can use a Unicode character similar to that symbol. Depending on what you want it for, this may be a valid solution.

Here you have a list of the Unicode characters similar to : that can be used in the name of a variable:

(https://www.compart.com/en/unicode/based/U+003A)

Based on the example you provided it would be this:

struct Metadata: Decodable{
    var metatags : [enclosedTags]
}
struct enclosedTags: Decodable{
    var image: String
    var title: String
    var description: String
    var og:site_name: String
}
nikano
  • 1,136
  • 1
  • 8
  • 18
0

Turns out swift has it's own feature in terms of naming specificity of variables in structs i.e. CodingKeys, so in terms for me the below naming convention worked,

   struct Metadata: Decodable{
        var metatags: [enclosedTags]

    }
        struct enclosedTags: Decodable{
            let image: String
            let title: String
            let description: String
            let siteName: String

            private enum CodingKeys : String, CodingKey{
                case image = "og:image", title = "og:title", description = "og:description", siteName = "og:site_name"
            }

This was rightfully pointed out by @hamish in comments (Thanks mate!)

Frostmourne
  • 156
  • 1
  • 19