Swift, Generics, 3

Generic Bir Class yazalım.

class GenericClass<T>
{
var oval : T?
init()
{

}

init(oval: T)
{
self.oval = oval
}
}
var oGenericClass1 : GenericClass = GenericClass<Int>()
var oGenericClass2 : GenericClass = GenericClass<String>(oval:"dirk")
var oGenericClass3 : GenericClass = GenericClass<Bool>(oval: true)
var oGenericClass4 : GenericClass = GenericClass<Any>(oval: false)
var oGenericClass5 : GenericClass = GenericClass<Double>(oval: 3.2)
var oGenericClass6 : GenericClass = GenericClass<Float>()
print("oGenericClass1.oval", oGenericClass1.oval) // nil
print("oGenericClass2.oval", oGenericClass2.oval!) // "dirk"
print("oGenericClass3.oval", oGenericClass3.oval) // Optional(true)
print("oGenericClass4.oval", oGenericClass4.oval) // Optional(false)
print("oGenericClass5.oval", oGenericClass5.oval) // Optional(3.2)
print("oGenericClass6.oval", oGenericClass6.oval) // nil

Referans: developer.apple.com
Makalenin yazıldığı zaman Swift versiyonu: 5.4
İyi çalışmalar.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store