0

Javascript developer here learning Swift. Normally in my JS projects I would have a nested object available as a helper for set variables and properties like this:

export const breakPoint = {
  mobile: 544,
  tablet: 768,
  laptop: 992,
  desktop: 1200,
  desktop_xl: 1400,
  nestedData: {
     data: "example"
  }
}

Pretty simple right, and I can import it into other files and then use like

breakPoint.mobile

I'm at a loss as to how to use a similar concept in Swift. Do I use Dictionaries or Tuples? Struct and initialize it?

Thank you in advance!

3 Answers 3

1

If I understand your question correctly Struct is a better option for that. Check below code for that:

This way you can create Struct:

struct BreakPoint {
    let mobile: Int
    let tablet: Int
    let laptop: Int
    let desktop: Int
    let desktop_xl: Int
    let nestedData: [String: String]
}

Then assign values to it:

let breakPoint = BreakPoint(mobile: 544, tablet: 768, laptop: 992, desktop: 1200, desktop_xl: 1400, nestedData: ["data": "example"])

Then you can access it's property this way:

print(breakPoint.mobile) // 544
Sign up to request clarification or add additional context in comments.

Comments

1

IMO, We can use enum in this case like shown below.

enum breakpoint: Int {
    case mobile = 544
    case tablet = 768
    case laptop = 992
    case desktop = 1200
    case desktop_xl = 1400

    enum nestedData : String {
        case data = "example"
    }
}

If we want access the value of each enum values, we can use rawValue method like shown below.

let model: breakpoint = .mobile
let modelRawValue = model.rawValue
let anotherModelValue = breakpoint.nestedData.data.rawValue

We can also achieve similar thing using structs as well.

You can find good article on swift enum here

2 Comments

If the whole point of this is simply to store those values, then static variables are a better choice, because they can be of different types, unlike raw values.
Yes, they are having different raw types(Int and String) here. I did not get any compiler error or warning in playgrounds when nested enum have a different raw type. Also, i did not get any clarity from Apple docs as well w.r.t nested enums with different raw types. But if we want separate groupings for different data types, we can go for two separate enums in this case.
0

I think the best way is using NSUserDefaults if Swift 2 or below, or just UserDefaults in Swift 3+.

UserDefaults.standard.set("xyz", forKey: "Key")

Then, you can retrieve,

UserDefaults.standard.string(forKey: "Key")

Efficient code.

2 Comments

This had literally nothing to do with the question
He needs something to define global constants, so UserDefaults does this thing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.