Tag: 协议

如何使用协议

我有两个viewControllers 服务提供者 工作地点 在ServiceProvider我有一个文本字段和一个selectbutton。 当用户点击selectbutton时,它将redirect到具有location列表的WorkLocation 。 当用户select任何location ,然后点击WorkLocationselectbutton时,它将redirect到带有locationName和Id ServiceProvider页面。 在ServiceProvider页面中, locationName将显示在已经存在的文本字段中。 要执行此操作,我正在使用一个协议。 WorkLocation声明: #import <UIKit/UIKit.h> #import "WorkDetailCell.h" @protocol WorkLocationDetailViewDelegate <NSObject> @required -(void)organizationInfo:(NSString *) organization_id name:(NSString *)name; @end @interface WorkLocationDetailsViewController :UIViewController < UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate > { IBOutlet UITableView *workLocationTableView; IBOutlet UISearchBar *workLocationSearchBar; NSMutableArray *workDetailArray,*tempDataArray,*searchArray,*searchResultArray; BOOL search; } @property (nonatomic,retain) id < WorkLocationDetailViewDelegate > delegate; WorkLocation实现: […]

如何在方法参数中使用Rawtypes遵守协议方法?

protocol Measurement { mutating func convert(#toUnit: String) } enum MassUnit : String { case Milligram = "mg" } enum VolumeUnit : String { case Milliliter = "ml" } struct Mass : Measurement { mutating func convert(#toUnit: MassUnit) // Build error: Does not adhere to 'Measurement' } struct Volume : Measurement { mutating func convert(#toUnit: VolumeUnit) […]

如何在AppDelegate iPhone应用程序中使用@protocol?

我在5屏幕的iPhone应用程序工作。 我想刷新UITabBarController中第四屏幕的值。 我已经在AppDelegate中添加@protocol,但它不是调用。 这是我第一次使用@protocol可以请你帮我解决这个问题, 在AppDelegate.h中 @protocol ReloadViewControllerDelegate <NSObject> -(void) refreshViewController:(NSString *)result; @end id refreshViewControllerDelegate; @property (nonatomic, retain) id refreshViewControllerDelegate; 我已经综合了 在AppDelegare.m @synthesize refreshViewControllerDelegate; if ([refreshViewControllerDelegate conformsToProtocol:@protocol(ReloadViewControllerDelegate)]) { [refreshViewControllerDelegate performSelectorOnMainThread:@selector(refreshViewController:) withObject:@"YES" waitUntilDone:NO]; }// Control not come inside of if Condition…. From here i want to update the fourthViewController.. 但是control not go inside of the if condition […]

为什么委托方法需要在Swift中公开?

我正在迅速开发一个框架。 我正在创build一个在框架中处理BLE东西的类。 这个类应该是公开的,因为我需要从使用我的框架的外部应用程序访问这个类。 我的课堂结构如下: public class MyClass: NSObject, CBCentralManagerDelegate { } 这里MyClass是公共的,它确认了一个公共协议CBCentralManagerDelegate 。 编译器迫使我把它的委托方法声明为public。 所以这里是我的实现看起来像: public class MyClass: NSObject, CBCentralManagerDelegate { public func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { } // I don't want delegate methods to be public, delegate methods should be like: //func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { //} } 我在这里探索苹果文件, 这里是一个SO的问题。 […]

如何将协议types添加为子视图

所以我写了一个简单的协议: protocol PopupMessageType{ var cancelButton: UIButton {get set} func cancel() } 并有一个customView: class XYZMessageView: UIView, PopupMessageType { … } 然后我现在有: class PopUpViewController: UIViewController { //code… var messageView : CCPopupMessageView! private func setupUI(){ view.addSubview(messageView) } } 但是我想要做的是: class PopUpViewController: UIViewController { //code… var messageView : PopupMessageType! private func setupUI(){ view.addSubview(messageView) // ERROR } } 错误我得到: 无法转换types“PopupMessageType!”的值 […]

XCode在尝试使用私有Pod(Swift)中定义的协议时说“使用未声明的types”

我和我的团队陷入了一个奇怪的情况,涉及一个基础项目+内部有一些协议的私人Pod。 我们的问题是,我们无法从我们的基本应用程序代码访问一些(协议)标识符(在私人pod中定义)。 显然我们的问题似乎与这两个堆栈溢出线程中所描述的完全相同,但是他们的解决scheme并没有与我们合作。 线程1: 在Swift中找不到源文件 线程2: 安装了CocoaPod,但没有看到Swift代码 我们使用的骨架是这个: 我们的Commons pod(使用我们无法从基本应用程序中看到的协议)使用此.podspec文件进行定义: Pod::Spec.new do |s| s.name = "Commons" s.version = "0.1.10" s.summary = "Commons framework" s.description = <<-DESC "Commons framework" DESC s.homepage = "http://EXAMPLE/Commons" s.license = { :type => "Commercial" } s.author = { "Author" => "author@mail.mail" } s.source = { :git => "GITLAB_PRIVATE_URL/commons-iOS-Pod.git", :tag => s.version […]

Swift:使用未声明的types

我正在实现一个类,但我得到这个错误: 使用未声明的types“myProtocol” 这是我的代码: class LocalContactService: myProtocol{ 你们谁都知道我为什么得到这个错误?

所有符合协议的类inheritance默认实现

我已经为我的所有UIViewController子类添加了一个方法,它允许我从类和它内部的故事板实例化它。 所有的方法都遵循这种格式: class func instantiateFromStoryboard() -> CameraViewController? { let storyboard = UIStoryboard(name: "Camera", bundle: nil) let initial = storyboard.instantiateInitialViewController() guard let controller = initial as? CameraViewController else { return nil } return controller } 相反 ,我想做一个协议, Instantiatable ,需要上面的方法以及一个variablesstoryboardName: String 。 然后,我想扩展这个Instantiatable所以它包含一个类似于上面的实现。 我的目标是我可以说,一个UIViewController坚持这个协议,我所要定义的是storyboardName 。 我觉得我接近这个实现: protocol Instantiatable { var storyboardName: String { get } func instantiateFromStoryboard() […]

NSManagedObject在Swift中实现协议的EXC_BAD_ACCESS错误

我有以下两种方法: func isAuthenticated() -> Bool { var currentUser: CurrentUser? = self.getCurrentUser() if currentUser == nil { return false } self.token = getUserToken(currentUser!.username) if self.token == nil { return false } if !tokenIsValidForUser(self.token!, user: currentUser!) { return false } return true } func tokenIsValidForUser(token: AuthenticationToken, user: UserObject) -> Bool { if token.username != user.username { return […]

以协议types为关键字的Swift Dictionary

这里简短的问题: 我得到一个协议protocol SCResourceModel {..}和一个字典,它应该使用这个协议types作为关键字: [SCResourceModel : String] 。 这显然不起作用,因为字典中的关键字必须符合协议Hashable 。 使我的SCResourceModel从SCResourceModelinheritance或尝试像这样[protocol<SCResourceModel, Hashable> : String]显然不能正常工作,因为Hashable或Equatable只能用作通用约束,而不是作为types本身。 我观看了WWDC 2015,并且在Swift 2.0中,可以将约束添加到如下协议: protocol SCResourceModel where Self: Hashable {..}直接解决了这个问题(非常好)。 无论如何,我的问题是:我可以做一些类似的当前Swift 1.2版本,并以某种方式使用此协议作为字典的关键? 或者任何人都可以提出一个很好的解决方法或其他我可能忽略的东西? 我目前看到的Swift 1.2的唯一解决scheme是将协议转换为inheritance自NSObject的类,并且必须在我的API中进一步使用。 谢谢你的帮助!