Constructors in Swift?











up vote
1
down vote

favorite
1












In Java we can use constructors in order to pass initial values into a class. Is this possible in swift?



For example, in the line below I am trying to add an object, which should include all the values you can see in the function that is within it, into an array called arrayOfMedia.



self.arrayOfMedia.append(Media().getUsersMedia(image: image!, postNum: anyPosts.key, userID: user))


I cannot do this however and get the error below.




Cannot convert value of type '()' to expected argument type 'Media'











share|improve this question
























  • Yes, they are called initialisers, the problem you have is, in you example, Media doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is, getUsersMedia is probably retuning void, hence you can't add it to the array
    – MadProgrammer
    Nov 8 at 23:11










  • I'm not familiar with Media, but are you asking if you can create a convenience initializer?
    – dfd
    Nov 8 at 23:12










  • Can you show what's returned from getUsersMedia?
    – sloik
    Nov 8 at 23:17










  • getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
    – nebur RB
    Nov 8 at 23:25










  • "I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
    – MadProgrammer
    Nov 8 at 23:28















up vote
1
down vote

favorite
1












In Java we can use constructors in order to pass initial values into a class. Is this possible in swift?



For example, in the line below I am trying to add an object, which should include all the values you can see in the function that is within it, into an array called arrayOfMedia.



self.arrayOfMedia.append(Media().getUsersMedia(image: image!, postNum: anyPosts.key, userID: user))


I cannot do this however and get the error below.




Cannot convert value of type '()' to expected argument type 'Media'











share|improve this question
























  • Yes, they are called initialisers, the problem you have is, in you example, Media doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is, getUsersMedia is probably retuning void, hence you can't add it to the array
    – MadProgrammer
    Nov 8 at 23:11










  • I'm not familiar with Media, but are you asking if you can create a convenience initializer?
    – dfd
    Nov 8 at 23:12










  • Can you show what's returned from getUsersMedia?
    – sloik
    Nov 8 at 23:17










  • getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
    – nebur RB
    Nov 8 at 23:25










  • "I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
    – MadProgrammer
    Nov 8 at 23:28













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





In Java we can use constructors in order to pass initial values into a class. Is this possible in swift?



For example, in the line below I am trying to add an object, which should include all the values you can see in the function that is within it, into an array called arrayOfMedia.



self.arrayOfMedia.append(Media().getUsersMedia(image: image!, postNum: anyPosts.key, userID: user))


I cannot do this however and get the error below.




Cannot convert value of type '()' to expected argument type 'Media'











share|improve this question















In Java we can use constructors in order to pass initial values into a class. Is this possible in swift?



For example, in the line below I am trying to add an object, which should include all the values you can see in the function that is within it, into an array called arrayOfMedia.



self.arrayOfMedia.append(Media().getUsersMedia(image: image!, postNum: anyPosts.key, userID: user))


I cannot do this however and get the error below.




Cannot convert value of type '()' to expected argument type 'Media'








arrays swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 1:43

























asked Nov 8 at 23:07









nebur RB

378




378












  • Yes, they are called initialisers, the problem you have is, in you example, Media doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is, getUsersMedia is probably retuning void, hence you can't add it to the array
    – MadProgrammer
    Nov 8 at 23:11










  • I'm not familiar with Media, but are you asking if you can create a convenience initializer?
    – dfd
    Nov 8 at 23:12










  • Can you show what's returned from getUsersMedia?
    – sloik
    Nov 8 at 23:17










  • getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
    – nebur RB
    Nov 8 at 23:25










  • "I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
    – MadProgrammer
    Nov 8 at 23:28


















  • Yes, they are called initialisers, the problem you have is, in you example, Media doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is, getUsersMedia is probably retuning void, hence you can't add it to the array
    – MadProgrammer
    Nov 8 at 23:11










  • I'm not familiar with Media, but are you asking if you can create a convenience initializer?
    – dfd
    Nov 8 at 23:12










  • Can you show what's returned from getUsersMedia?
    – sloik
    Nov 8 at 23:17










  • getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
    – nebur RB
    Nov 8 at 23:25










  • "I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
    – MadProgrammer
    Nov 8 at 23:28
















Yes, they are called initialisers, the problem you have is, in you example, Media doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is, getUsersMedia is probably retuning void, hence you can't add it to the array
– MadProgrammer
Nov 8 at 23:11




Yes, they are called initialisers, the problem you have is, in you example, Media doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is, getUsersMedia is probably retuning void, hence you can't add it to the array
– MadProgrammer
Nov 8 at 23:11












I'm not familiar with Media, but are you asking if you can create a convenience initializer?
– dfd
Nov 8 at 23:12




I'm not familiar with Media, but are you asking if you can create a convenience initializer?
– dfd
Nov 8 at 23:12












Can you show what's returned from getUsersMedia?
– sloik
Nov 8 at 23:17




Can you show what's returned from getUsersMedia?
– sloik
Nov 8 at 23:17












getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
– nebur RB
Nov 8 at 23:25




getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
– nebur RB
Nov 8 at 23:25












"I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
– MadProgrammer
Nov 8 at 23:28




"I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
– MadProgrammer
Nov 8 at 23:28












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted











I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)




Okay, so that makes no sense, you have a class which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?



There's a few ways you "might" achieve this, this is just one...



class Media {
var image: UIImage?
var video: Data?
let postKey: Int
let userId: Int

internal required init(postKey: Int, userId: Int) {
self.postKey = postKey
self.userId = userId
}

convenience init(image: UIImage, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.image = image
}

convenience init(video: Data, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.video = video
}
}


Also, note, you could have simply provided a single initialiser, something like...



init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}


but this doesn't constraint the user to one or the other type (they can still pass nil for both values)



Another approach might be to make use of a protocol to describe the basic/common properties of Media and then implement the different requirements (directly as structs or classs or indirectly as additional protocols)



For example...



protocol Media  {
var postKey: Int { get }
var userId: Int { get }
}

struct VideoMedia: Media {
let postKey: Int
let userId: Int
let video: Data
}

struct ImageMedia: Media {
let postKey: Int
let userId: Int
let image: UIImage
}





share|improve this answer



















  • 1




    SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
    – nebur RB
    Nov 9 at 0:36










  • If you have a reference to the Media object, why not reference the image property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
    – MadProgrammer
    Nov 9 at 0:40












  • I will add new question that adresses this as I think it is not good here or do you think otherwise?
    – nebur RB
    Nov 9 at 0:43






  • 1




    @neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
    – MadProgrammer
    Nov 9 at 0:57










  • stackoverflow.com/questions/53234459/… Here is teh question
    – nebur RB
    Nov 9 at 23:16


















up vote
1
down vote













For what it's worth, in this case I would use enums to wrap your media types.



enum MediaType {
case image(UIImage)
case video(Data)
}


Then you have Type safe access without requiring optionals:



struct Media {
let postKey: Int
let userId: Int
let mediaType: MediaType
}

let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217525%2fconstructors-in-swift%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted











    I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)




    Okay, so that makes no sense, you have a class which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?



    There's a few ways you "might" achieve this, this is just one...



    class Media {
    var image: UIImage?
    var video: Data?
    let postKey: Int
    let userId: Int

    internal required init(postKey: Int, userId: Int) {
    self.postKey = postKey
    self.userId = userId
    }

    convenience init(image: UIImage, postKey: Int, userId: Int) {
    self.init(postKey: postKey, userId: userId)
    self.image = image
    }

    convenience init(video: Data, postKey: Int, userId: Int) {
    self.init(postKey: postKey, userId: userId)
    self.video = video
    }
    }


    Also, note, you could have simply provided a single initialiser, something like...



    init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}


    but this doesn't constraint the user to one or the other type (they can still pass nil for both values)



    Another approach might be to make use of a protocol to describe the basic/common properties of Media and then implement the different requirements (directly as structs or classs or indirectly as additional protocols)



    For example...



    protocol Media  {
    var postKey: Int { get }
    var userId: Int { get }
    }

    struct VideoMedia: Media {
    let postKey: Int
    let userId: Int
    let video: Data
    }

    struct ImageMedia: Media {
    let postKey: Int
    let userId: Int
    let image: UIImage
    }





    share|improve this answer



















    • 1




      SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
      – nebur RB
      Nov 9 at 0:36










    • If you have a reference to the Media object, why not reference the image property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
      – MadProgrammer
      Nov 9 at 0:40












    • I will add new question that adresses this as I think it is not good here or do you think otherwise?
      – nebur RB
      Nov 9 at 0:43






    • 1




      @neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
      – MadProgrammer
      Nov 9 at 0:57










    • stackoverflow.com/questions/53234459/… Here is teh question
      – nebur RB
      Nov 9 at 23:16















    up vote
    3
    down vote



    accepted











    I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)




    Okay, so that makes no sense, you have a class which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?



    There's a few ways you "might" achieve this, this is just one...



    class Media {
    var image: UIImage?
    var video: Data?
    let postKey: Int
    let userId: Int

    internal required init(postKey: Int, userId: Int) {
    self.postKey = postKey
    self.userId = userId
    }

    convenience init(image: UIImage, postKey: Int, userId: Int) {
    self.init(postKey: postKey, userId: userId)
    self.image = image
    }

    convenience init(video: Data, postKey: Int, userId: Int) {
    self.init(postKey: postKey, userId: userId)
    self.video = video
    }
    }


    Also, note, you could have simply provided a single initialiser, something like...



    init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}


    but this doesn't constraint the user to one or the other type (they can still pass nil for both values)



    Another approach might be to make use of a protocol to describe the basic/common properties of Media and then implement the different requirements (directly as structs or classs or indirectly as additional protocols)



    For example...



    protocol Media  {
    var postKey: Int { get }
    var userId: Int { get }
    }

    struct VideoMedia: Media {
    let postKey: Int
    let userId: Int
    let video: Data
    }

    struct ImageMedia: Media {
    let postKey: Int
    let userId: Int
    let image: UIImage
    }





    share|improve this answer



















    • 1




      SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
      – nebur RB
      Nov 9 at 0:36










    • If you have a reference to the Media object, why not reference the image property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
      – MadProgrammer
      Nov 9 at 0:40












    • I will add new question that adresses this as I think it is not good here or do you think otherwise?
      – nebur RB
      Nov 9 at 0:43






    • 1




      @neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
      – MadProgrammer
      Nov 9 at 0:57










    • stackoverflow.com/questions/53234459/… Here is teh question
      – nebur RB
      Nov 9 at 23:16













    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted







    I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)




    Okay, so that makes no sense, you have a class which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?



    There's a few ways you "might" achieve this, this is just one...



    class Media {
    var image: UIImage?
    var video: Data?
    let postKey: Int
    let userId: Int

    internal required init(postKey: Int, userId: Int) {
    self.postKey = postKey
    self.userId = userId
    }

    convenience init(image: UIImage, postKey: Int, userId: Int) {
    self.init(postKey: postKey, userId: userId)
    self.image = image
    }

    convenience init(video: Data, postKey: Int, userId: Int) {
    self.init(postKey: postKey, userId: userId)
    self.video = video
    }
    }


    Also, note, you could have simply provided a single initialiser, something like...



    init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}


    but this doesn't constraint the user to one or the other type (they can still pass nil for both values)



    Another approach might be to make use of a protocol to describe the basic/common properties of Media and then implement the different requirements (directly as structs or classs or indirectly as additional protocols)



    For example...



    protocol Media  {
    var postKey: Int { get }
    var userId: Int { get }
    }

    struct VideoMedia: Media {
    let postKey: Int
    let userId: Int
    let video: Data
    }

    struct ImageMedia: Media {
    let postKey: Int
    let userId: Int
    let image: UIImage
    }





    share|improve this answer















    I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)




    Okay, so that makes no sense, you have a class which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?



    There's a few ways you "might" achieve this, this is just one...



    class Media {
    var image: UIImage?
    var video: Data?
    let postKey: Int
    let userId: Int

    internal required init(postKey: Int, userId: Int) {
    self.postKey = postKey
    self.userId = userId
    }

    convenience init(image: UIImage, postKey: Int, userId: Int) {
    self.init(postKey: postKey, userId: userId)
    self.image = image
    }

    convenience init(video: Data, postKey: Int, userId: Int) {
    self.init(postKey: postKey, userId: userId)
    self.video = video
    }
    }


    Also, note, you could have simply provided a single initialiser, something like...



    init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}


    but this doesn't constraint the user to one or the other type (they can still pass nil for both values)



    Another approach might be to make use of a protocol to describe the basic/common properties of Media and then implement the different requirements (directly as structs or classs or indirectly as additional protocols)



    For example...



    protocol Media  {
    var postKey: Int { get }
    var userId: Int { get }
    }

    struct VideoMedia: Media {
    let postKey: Int
    let userId: Int
    let video: Data
    }

    struct ImageMedia: Media {
    let postKey: Int
    let userId: Int
    let image: UIImage
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 0:27

























    answered Nov 9 at 0:21









    MadProgrammer

    297k17152263




    297k17152263








    • 1




      SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
      – nebur RB
      Nov 9 at 0:36










    • If you have a reference to the Media object, why not reference the image property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
      – MadProgrammer
      Nov 9 at 0:40












    • I will add new question that adresses this as I think it is not good here or do you think otherwise?
      – nebur RB
      Nov 9 at 0:43






    • 1




      @neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
      – MadProgrammer
      Nov 9 at 0:57










    • stackoverflow.com/questions/53234459/… Here is teh question
      – nebur RB
      Nov 9 at 23:16














    • 1




      SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
      – nebur RB
      Nov 9 at 0:36










    • If you have a reference to the Media object, why not reference the image property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
      – MadProgrammer
      Nov 9 at 0:40












    • I will add new question that adresses this as I think it is not good here or do you think otherwise?
      – nebur RB
      Nov 9 at 0:43






    • 1




      @neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
      – MadProgrammer
      Nov 9 at 0:57










    • stackoverflow.com/questions/53234459/… Here is teh question
      – nebur RB
      Nov 9 at 23:16








    1




    1




    SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
    – nebur RB
    Nov 9 at 0:36




    SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
    – nebur RB
    Nov 9 at 0:36












    If you have a reference to the Media object, why not reference the image property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
    – MadProgrammer
    Nov 9 at 0:40






    If you have a reference to the Media object, why not reference the image property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
    – MadProgrammer
    Nov 9 at 0:40














    I will add new question that adresses this as I think it is not good here or do you think otherwise?
    – nebur RB
    Nov 9 at 0:43




    I will add new question that adresses this as I think it is not good here or do you think otherwise?
    – nebur RB
    Nov 9 at 0:43




    1




    1




    @neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
    – MadProgrammer
    Nov 9 at 0:57




    @neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
    – MadProgrammer
    Nov 9 at 0:57












    stackoverflow.com/questions/53234459/… Here is teh question
    – nebur RB
    Nov 9 at 23:16




    stackoverflow.com/questions/53234459/… Here is teh question
    – nebur RB
    Nov 9 at 23:16












    up vote
    1
    down vote













    For what it's worth, in this case I would use enums to wrap your media types.



    enum MediaType {
    case image(UIImage)
    case video(Data)
    }


    Then you have Type safe access without requiring optionals:



    struct Media {
    let postKey: Int
    let userId: Int
    let mediaType: MediaType
    }

    let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
    let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))





    share|improve this answer

























      up vote
      1
      down vote













      For what it's worth, in this case I would use enums to wrap your media types.



      enum MediaType {
      case image(UIImage)
      case video(Data)
      }


      Then you have Type safe access without requiring optionals:



      struct Media {
      let postKey: Int
      let userId: Int
      let mediaType: MediaType
      }

      let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
      let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        For what it's worth, in this case I would use enums to wrap your media types.



        enum MediaType {
        case image(UIImage)
        case video(Data)
        }


        Then you have Type safe access without requiring optionals:



        struct Media {
        let postKey: Int
        let userId: Int
        let mediaType: MediaType
        }

        let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
        let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))





        share|improve this answer












        For what it's worth, in this case I would use enums to wrap your media types.



        enum MediaType {
        case image(UIImage)
        case video(Data)
        }


        Then you have Type safe access without requiring optionals:



        struct Media {
        let postKey: Int
        let userId: Int
        let mediaType: MediaType
        }

        let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
        let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 0:49









        PeejWeej

        4,88111438




        4,88111438






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217525%2fconstructors-in-swift%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff