Swift/iOS: How to access Object properties when type-casted from Protocol?

Swift/iOS: How to access Object properties when type-casted from Protocol?

Content Index :

Swift/iOS: How to access Object properties when type-casted from Protocol?
Tag : swift , By : nabbed
Date : November 29 2020, 01:01 AM

I think the issue was by ths following , The following post will help you better understand the issue

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Access associated type of a custom protocol in a where clause on generic types in Swift

Tag : generics , By : Pierre LeBoo
Date : March 29 2020, 07:55 AM
will help you The problem is that you're using the reserved word Type. Try it with some other name like HashType and it compiles fine.
See "Metatype Type" from the Swift Programming Language:

Can I cast a metaclass object to a protocol type in Swift?

Tag : swift , By : Angel Paunchev
Date : March 29 2020, 07:55 AM
it fixes the issue As of Xcode 7 beta 2 and Swift 2 it has been fixed. You can now write:
for type in types {
    if let fooType = type as? Foo.Type {
        // in Swift 2 you have to explicitly call the initializer of metatypes
        let obj = fooType.init(foo: "special snowflake string")
for case let type as Foo.Type in types {
    let obj = type.init(foo: "special snowflake string")

In swift, how do I return an object of the same type that conforms to a protocol

Tag : swift , By : Adil
Date : March 29 2020, 07:55 AM
around this issue Autocomplete will help you, but basically, if Foo is a class, you just need a method which matches the exact signature of the method in your protocol:
class Foo {}

protocol JSONInitializable {
    static func initialize(fromJSON: [String : AnyObject]) throws -> Self?

extension Foo: JSONInitializable {
    static func initialize(fromJSON: [String : AnyObject]) throws -> Self? {
        return nil
struct Foo: JSONInitializable {
    static func initialize(fromJSON: [String : AnyObject]) throws -> Foo? {
        return nil
enum Foo: JSONInitializable {
    case One, Two, Three

    static func initialize(fromJSON: [String : AnyObject]) throws -> Foo? {
        return nil

How to make a swift class conform to a protocol that requires a protocol-conform type for one of its properties?

Tag : swift , By : wrb302
Date : March 29 2020, 07:55 AM
I hope this helps you . You can do this with associated types.
Declare an associated type in ProtocolA:
protocol ProtocolA {
    associatedtype BType: ProtocolB
    var b: BType? { get } // Note the change in the type of b
extension A: ProtocolA {
    typealias BType = B
var protocolA: ProtocolA? // error

Swift cast object to type and protocol at the same time

Tag : ios , By : Alan Little
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Upgrading WKInterfaceController classes to new SwiftUI struct/View
  • TabbedView using SwiftUI in Xcode11Beta (11M336w)
  • IndexSet referring to index of the section instead of the row
  • Swift UI - How to make Image Grids?
  • Enum as a key for dictionary
  • SwiftUI - NavigationView Error message - Argument passed to call that takes no arguments
  • Does guard make a copy of variables in swift
  • Error "Argument labels '(min:, max:)' do not match any available overloads"
  • Struct Memberwise Initialization - Omitting values for properties that have defaults
  • How to get RGB components from Color SwiftUI
  • Why do Singleton's capture an instance of themselves?
  • Firebase Rules - Restrict to a specific user or only if authorized?
  • Is there a way I can combine a GeoFireStore query with a normal Firestore query?
  • SwiftUI TextField Simple Example not working
  • Initializers may only be declared within a type
  • memory leak with swift due to reference cycle
  • addUIInterruptionMonitor is not getting called on macOS
  • Show user location on map SwiftUI
  • What is the recommended logic to use when checking authenticity in Auto Renewal Subscriptions
  • Unable to deploy Swift Vapor app in gCloud
  • Project Euler Problem #2 in Swift Sum of Fibonacci Numbers Below 4 Million
  • Why return nil tableViewCell for UITableViewDiffableDataSource cellProvider param?
  • Xcode 11 beta and Mac OS Mojave: when I try to create a new project and/or open existing one it closes down with error
  • In Swift, is it a good practice to declare variables with a value assigned?
  • Deriving binding from existing SwiftUI @States
  • Advice for updating old Swift Project
  • Swift AVAssetWriter Record Video with Mic Audio AND Device Audio into a Video with ONE Audio Track AVAssetTrack
  • SwiftUI dismiss modal
  • VStack not filling screen width in ScrollView, does fill in List
  • How to detect a tap gesture location in SwiftUI?
  • Using Xcode/Swift, how can I use an SF Symbol as a tab bar icon in assets? The asset field won't accept the .svg file
  • How to fix Xcode error "Segmentation fault: 11" after adding didSet to @State var
  • Views compressed by other views in SwiftUI VStack and List
  • Why is the modifier .navigationBarTitle not applied to NavigationView?
  • "Cannot Preview in this file - Failed to launch [App Name]"
  • Get the current position of ScrollView in SwiftUI?
  • How to use buildExpression in Swift 5.2 Function Builders?
  • Where do I put a return in a function?
  • SwiftUI - Is there a popViewController equivalent in SwiftUI?
  • How to remove "row" separators from a List in SwiftUI?
  • Understanding @Binding in SwiftUI
  • Convert Realm list of Strings to Array of Strings in Swift
  • Are the #if DEBUG statements really needed for previews in SwiftUI to remove it in a release build?
  • WkWebView in MacOS remains blank on local or remote content
  • Set Toggle color in SwiftUI
  • Reduce a double in swift
  • Trouble Arranging SceneKit Nodes into a Grid
  • Combine in Playgrounds
  • Singleton class can be created without using 'Static'?
  • How do I fix this '<=' is not a prefix unary operator error?
  • What is the property / $property syntax for bindings?
  • Where should I place the process that don't have relation to UI
  • What does the attribute `@EnvironmentObject` mean in the SwiftUI?
  • Is SwiftUI backwards-compatible with iOS 12.x and older?
  • Swift enums with stored fields?
  • Is there a way to pass the object of two different class in one variable in different conditions
  • How to sync input and playback for core audio using swift
  • UserDefaults not working on didload in my project
  • DispatchQueue and [weak self] workItems. Is [weak self] necessary?
  • remove items at multiple indexes in an array
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com