[swift-evolution] [Proposal]Add support for extension variables

Yogev Sitton yogev.sitton at gmail.com
Mon Apr 4 05:00:50 CDT 2016


Hi,

I try to avoid using inheritance for just adding functionality - for that I use (and love) extensions.
This works great for functions - but I find the lack for stored properties support limiting.

Here’s my use case:
I want to map every textfield in my view controller to a JSON field - so it would be very helpful to add a string Key property to all of the UITextFields in my app.
For now this is how I solve this issue (inside the extension):

struct customProperties {
	static var key : String?
}
	
var key : String? {
	get {
		return objc_getAssociatedObject(self, &customProperties.key) as? String
	}
	set (value){
		objc_setAssociatedObject(self,&customProperties.key,value,.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
	}
}

I would love to just add an actual variable to the extension without the need to use Obj-C associated objects.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160404/2d8d3ba9/attachment.html>


More information about the swift-evolution mailing list