By nebur RB


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?

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

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'

2 comments

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

@nebur RB 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

@nebur RB 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

@nebur RB 2018-11-09 23:16:04

Related Questions

Sponsored Content

17 Answered Questions

[SOLVED] #pragma mark in Swift?

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

8 Answered Questions

[SOLVED] Swift Beta performance: sorting arrays

12 Answered Questions

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

  • 2014-06-04 03:19:21
  • metaphy
  • 313925 View
  • 635 Score
  • 12 Answer
  • Tags:   arrays swift

15 Answered Questions

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

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

1 Answered Questions

[SOLVED] Initializing an array of Doubles in Swift 4

  • 2018-03-02 15:33:49
  • Adrian
  • 158 View
  • 3 Score
  • 1 Answer
  • Tags:   arrays swift

3 Answered Questions

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

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

1 Answered Questions

[SOLVED] Array of Enums in Swift 3

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

5 Answered Questions

[SOLVED] How do I use arrays in C++?

0 Answered Questions

Encoding Swift Array

2 Answered Questions

[SOLVED] Swift Passing array of dictionary to NSMutableArray

Sponsored Content