By user10281448

2018-11-08 23:07:45 8 Comments

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'


@PeejWeej 2018-11-09 00:49:05

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))

@MadProgrammer 2018-11-09 00:21:26

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) = 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

@user10281448 2018-11-09 00:36:06

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?

@MadProgrammer 2018-11-09 00:40:03

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

@user10281448 2018-11-09 00:43:50

I will add new question that adresses this as I think it is not good here or do you think otherwise?

@MadProgrammer 2018-11-09 00:57:04

@neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful

@user10281448 2018-11-09 23:16:04

Related Questions

Sponsored Content

13 Answered Questions

[SOLVED] swift for loop: for index, element in array?

  • 2014-06-04 03:19:21
  • metaphy
  • 344930 View
  • 674 Score
  • 13 Answer
  • Tags:   arrays swift

17 Answered Questions

[SOLVED] #pragma mark in Swift?

  • 2014-06-03 14:05:56
  • Arbitur
  • 200925 View
  • 867 Score
  • 17 Answer
  • Tags:   swift

9 Answered Questions

[SOLVED] Swift Beta performance: sorting arrays

15 Answered Questions

[SOLVED] How to call Objective-C code from Swift

  • 2014-06-02 20:05:42
  • David Mulder
  • 258066 View
  • 884 Score
  • 15 Answer
  • Tags:   objective-c swift

17 Answered Questions

[SOLVED] How to initialize an array's length in javascript?

  • 2011-01-31 14:29:07
  • Michael Martin-Smucker
  • 488389 View
  • 405 Score
  • 17 Answer
  • Tags:   javascript arrays jslint

7 Answered Questions

7 Answered Questions

[SOLVED] Are arrays passed by value or passed by reference in Java?

  • 2012-10-06 07:44:15
  • Froskoy
  • 164796 View
  • 141 Score
  • 7 Answer
  • Tags:   java arrays

3 Answered Questions

[SOLVED] Swift Error: Cannot convert value of type 'ArraySlice' to expected argument type

  • 2016-01-27 04:15:11
  • chris P
  • 8146 View
  • 15 Score
  • 3 Answer
  • Tags:   arrays swift

1 Answered Questions

[SOLVED] Array of Enums in Swift 3

  • 2017-09-11 01:58:11
  • Maria 9905
  • 470 View
  • 0 Score
  • 1 Answer
  • Tags:   swift

2 Answered Questions

[SOLVED] Swift Passing array of dictionary to NSMutableArray

Sponsored Content