0

Question: how would one go about initializing objects using default property values? Can one override an init within a class to create the objects?

I want to avoid using the view controller to input the object's property data. I am not pulling in data from an external source. Just want to keep the data in a separate file, like a class file, because the application involves a ton of text.

The objects I have in mind would be something that looks like a typical class object with a number of properties: object1(prop1:"string", prop2: [string], prop3: [string] and so on ...) but created in the same class to be inserted into an array of objects.

P.Festus
  • 95
  • 2
  • 11

2 Answers2

0

Use a caseless enum, kind of like a name space, to store your text. Then just reference those in the default initializers.

enum StaticText {
    static let A = "A"
    static let B = "B"
    static let C = "C"
}


class SomeClass {
    let A = StaticText.A
    let B = StaticText.B
    let C = StaticText.C
}
Alexander
  • 59,041
  • 12
  • 98
  • 151
  • If static value is what you only need, why do you use enum instead of struct or class with static? – Ryan Nov 22 '16 at 22:28
  • That's the current Swift convention. It's the closest thing Swift has to a namespace. Enums can't be accidentally instantiated. – Alexander Nov 22 '16 at 22:38
  • 1
    http://stackoverflow.com/a/38585994/3141234 https://github.com/raywenderlich/swift-style-guide#constants – Alexander Nov 22 '16 at 22:39
  • Great article. Thanks! – Ryan Nov 22 '16 at 22:45
  • Interesting. I'll give it a try. However, the objects I have in mind would be something that looks like a typical class object with a number of properties: object1(prop1:"string", prop2: [string], prop3: [string] and so on ... ). My thought was to create a class with an initializer and then use the initializer /maybe override?/ to create the objects in the same class rather than in a view controller object to avoid clutter. – P.Festus Nov 22 '16 at 23:33
  • @P.Festus That's hard to follow without seeing it – Alexander Nov 22 '16 at 23:53
0

Make an extension that uses a convenience initializer:

public class ToolBoardView: UIVisualEffectView {

    public var closeButton = ToolBoardCloseButton()
    public var imageSegments = UISegmentedControl (items: ["Subject","Frame"])
    internal var sliderBackgrounds:[UILabel] = []

    convenience internal init(
        _ tag         :Int,
        _ p: inout [NSLayoutConstraint],
        _ l: inout [NSLayoutConstraint],
        _ target      :Any,
        _ hideAction  :Selector
        )
    {
        self.init(frame: CGRect.zero)
        self.effect = UIBlurEffect(style: .light)
        self.tag = tag
        switch tag {
        case 0:
            break
        case 1:
            addFilterControls(&p, &l)
        default:
            break
        }
        closeButton = ToolBoardCloseButton(tag: tag, target: target, action: hideAction)
        self.addSubview(closeButton)
        turnOffAutoResizing()
        makeControlsHidden(true)
    }
}

That's just part of one I have, but it should get you going.

Here is a very good description of what I just posted.