protocols

How can I “explicitly” implement a protocol in swift? If it is impossible, why?

无人久伴 提交于 2020-01-11 06:40:31
问题 In C#, there is this great language feature called "explicit interface implementations" that allows you to implement two or more interfaces where the names of the interfaces' methods conflict. It can also make a method do one thing when you call it using an object of the enclosing type, and do another thing when you cast it to the interface type then call the method. I am wondering if there is such a thing in Swift. Does this conflict with any of swift's ideologies? Basically I want to do

Require type and protocol for method parameter

亡梦爱人 提交于 2020-01-10 08:41:40
问题 I am playing around with Swift and am stumbling over the following problem: given I have the predefined class Animal : //Predefined classes class Animal { var height: Float = 0.0 } I now write the class Zoo with the constructor accepting animals. But the Zoo wants every animal to have a name and hence defines the Namable protocol. protocol Namable { var name: String {get} } class Zoo { var animals: Animal[] = []; } How would you write an addAnimal method that requires the object being passed

Require type and protocol for method parameter

送分小仙女□ 提交于 2020-01-10 08:41:07
问题 I am playing around with Swift and am stumbling over the following problem: given I have the predefined class Animal : //Predefined classes class Animal { var height: Float = 0.0 } I now write the class Zoo with the constructor accepting animals. But the Zoo wants every animal to have a name and hence defines the Namable protocol. protocol Namable { var name: String {get} } class Zoo { var animals: Animal[] = []; } How would you write an addAnimal method that requires the object being passed

Implementing a function with a default parameter defined in a protocol

拜拜、爱过 提交于 2020-01-09 11:11:06
问题 Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback" : It's executed when a type conforms to the protocol but doesn't provide its own implementation. At least that's how I read The Swift Programming Language guide: If a conforming type provides its own implementation of a required method or property, that

Implementing a function with a default parameter defined in a protocol

一曲冷凌霜 提交于 2020-01-09 11:09:29
问题 Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback" : It's executed when a type conforms to the protocol but doesn't provide its own implementation. At least that's how I read The Swift Programming Language guide: If a conforming type provides its own implementation of a required method or property, that

How to expose existing property on Obj-C class using an extension protocol in Swift

情到浓时终转凉″ 提交于 2020-01-09 08:06:30
问题 In Swift 1.1 we were able to have code like below compile and work where we exposed existing Objective-C properties through a protocol added by an extension. We also had a few where the property is handled by the extension. @objc protocol Enableable: class { var enabled: Bool { get set } } let DisabledAlpha: CGFloat = 0.5 let EnabledAlpha: CGFloat = 1.0 extension UIButton: Enableable {} extension UIImageView: Enableable { var enabled: Bool { get { return alpha > DisabledAlpha } set(enabled) {

How to expose existing property on Obj-C class using an extension protocol in Swift

自闭症网瘾萝莉.ら 提交于 2020-01-09 08:06:05
问题 In Swift 1.1 we were able to have code like below compile and work where we exposed existing Objective-C properties through a protocol added by an extension. We also had a few where the property is handled by the extension. @objc protocol Enableable: class { var enabled: Bool { get set } } let DisabledAlpha: CGFloat = 0.5 let EnabledAlpha: CGFloat = 1.0 extension UIButton: Enableable {} extension UIImageView: Enableable { var enabled: Bool { get { return alpha > DisabledAlpha } set(enabled) {

how to upload File in angularjs frontend

一世执手 提交于 2020-01-07 07:13:23
问题 I want to Upload a data api with the following signature but I want to double check how the data is received.I have used just ordinary modules in angular to upload the file but I want to check how the file pushed to the api. I want the file to be collection of bytes as it reaches the api but here am uploading the just the file. Does the internal transfer protocol change it to bytes ? notice the file has type of collection of bytes. How should I upload that 回答1: For this I use the ng-file

NSDate's initWithString: is returning nil

眉间皱痕 提交于 2020-01-07 04:32:14
问题 I'm using Stig Brautaset's JSON framework serialize some objects, including NSDates (which are not directly supported). I decided to use NSDate's description as the JSONFragment representation of a date (I don't care about the minor loss of precision incurred in doing so). To extend Stig Brautaset's JSON framework to include NSDates, I defined a category: @interface NSDate (NSDate_JSON) <JSONInitializer> -(NSString *) JSONFragment; @end To recreate an NSDate (and other classes) from JSON, I

ios Swift - API GoogleMaps - not conform protocol GSMAutocompleteViewControllerDelegate

妖精的绣舞 提交于 2020-01-07 03:05:38
问题 I'm trying to use API GoogleMaps in an ios app to make an auto-completion, but I can't get my hand on the problem, I search for it but it seems I'm the only one to have it. MyViewController doesn't conform to protocol GSMAutocompleteViewControllerDelegate class MyViewController: UIViewController { @IBAction func onLaunchClicked(sender: AnyObject) { let acController = GMSAutocompleteViewController() acController.delegate = self self.presentViewController(acController, animated: true,