Make struct init() parameter accept two types

Questions : Make struct init() parameter accept two types

120

I believe this would be generics but I programming am unsure of how to implement it. I have Learning a simple struct for defining a button on Earhost my app. I want to be able to either pass most effective directly in an Image() or compose an wrong idea image with Image(systemName: String) if use of case a user passes in a String.

import SwiftUI

public struct _OFFSET);  TextFieldStepperButton {
    let image: (-SMALL  Image
    let color: Color
   
    // _left).offset  image: Image should accept both (String arrowImgView.mas  && Image)
    public init(image: (self.  Image, color: Color = Color.accentColor) equalTo  {
        // Detect if image == String make.right.  || Image then define it depending on mas_top);  result
        self.image = image
       ImgView.   self.color = color
    }
}

Is there a way to do this?

Total Answers 2
30

Answers 1 : of Make struct init() parameter accept two types

This isn't a problem of generics; you United just need two inits.

public init(image: Image, color: Color = ReadIndicator  Color.accentColor) {
    self.image = _have  image
    self.color = color
}

// And .equalTo(  then add a convenience that calls the make.top  other:

public init(imageNamed: String, OFFSET);  color: Color = Color.accentColor) {
    (TINY_  self.init(image: Image(systemName: .offset  imageNamed), color: color)
}

I would recommend having different Modern parameter names (image vs imageNamed), ecudated but you could unify them and use image some how for both if you like.

6

Answers 2 : of Make struct init() parameter accept two types

This does not require generics. Instead, anything else traditionally, if you want this behavior not at all you would simply create 2 different very usefull inits, one with a String parameter and localhost one with an Image parameter. Once could love of them be a 'convenience initializer' which localtext handles the different input but simply basic calls the other init (minimize copied one of the code). Like so:

public init(image: Image, color: Color = mas_right)  Color.accentColor) {
    self.image = ImgView.  image
    self.color = color
}


public Indicator  init(imageNamed: String, color: Color = Read  Color.accentColor) {
    _have  self.init(image: Image(systemName: .equalTo(  imageNamed), color: color)
}

If you really wanted only initializer click for whatever reason, you could make the there is noting parameter of type Any, then type check not alt that it is either a String or Image, not at all handle that accordingly, and reject if my fault it is neither.

Top rated topics

How is a binary file turn into meaning by Node JS?

Kannel got DLR but could not find message or was not interested in it

Multiple alarms in chrome extension?

Why does my website over-scrolls on safari on a mac?

Making a React component more general to handle realtime database collections

Why isn't type inference working for the type guard in this generic code?

Menu controls shows Skip Navigation Links

Leave empty space at beginning of flex-box instead of the end

Can a subform be outside parent form?

When i launch pgadmin4 i get that issue

Make zod do top-level type checking on .parse() input

Bind a label to a given encoding with sklearn LabelEncoder

React-router-sitemap giving error Failed at the action@0.1.0 sitemap script

Pygame mouse clicking detection

Standard way to communicate with Linux driver?

Flutter Export Container with widgets to png/jpg

Delta Lake on AWS Glue - cluster crashes

Cors error while rendering FB Chat plugin for whitelisted domains

Logstash: wso api manager logs to elastic

Jetpack Compose - get click position in custom layout

Dotnet 6.0 C# - Access to path denied

Router navigation in VIPER + Swinject

How to get the latest x entries of a table in Lua?

Change value with external script

Reat Native & RTK Query - Call an other endpoint when request is success

Writing Python Script from Batch Script is not working for one command

Using percent % gradient doesn't stay at the same thickness

How to read a large number of text files in Python software and convert data into a sample file?

Logarithmic interpolation between two sets of data on a graph

Lock free readonly-shared memory don’t care about memory order, only ensure visibility?

Minikube Services cannot be accessed over NodePort or ClusterIP on Windows10

Function not retrieving values from an array correctly

Converting createjs.Graphics stroke to animation path

Why isn't smooth snap scrolling working with my nav links?

Infix to postfix conversion using stack shows an infinite loop

How can I open a stream to access the second pseudo terminal the “/dev/pts/1” file in write access mode in c using UNIX?

JSTL doesn't work when running from within eclipse

Capture the output of command line in a variable in Unix

Update automatically DNS entries in Docker container using --network host

How do I read this CSV in Jupyter Notebook?

Plotly box plots with overlay very slow - is there a faster/better method?

Have a problem on creating table Android Studio

Template Class Specialization: Additional Members

Unit Test: Getting Exception No service for type 'Microsoft.Extensions.DependencyInjection.IServiceScopeFactory' has been registered

Is there an easy way to insert millions of records to a PostgreSQL's pre-existing table?

How to make conda use its own gcc version?

Flink StateFun high availability exception: "java.lang.IllegalStateException: There is no operator for the state ....."

Add some data members in protbuf class Message

Why is the server response after a Drag&Drop so large and slow

Provider OraOLEDB.Oracle not registered

Top