所以我将Xcode和GitHub项目代码升级到swift 3.0(我的项目在Obj C中,我在GitHub中使用的一些pod都在Swift中)。我收到了一些错误,现在我被困在这些错误上了。由于某种原因,我的floatingActionButton(git here)的dataSource和委托现在不起作用。我尝试以编程方式和故事板设置dataSource
和delegate
,但它没有用。
错误:
在'LiquidFloatingActionButton *'类型的对象上找不到属性'dataSource'
在'LiquidFloatingActionButton *'类型的对象上找不到属性'委托'
我相信,如果我弄清楚dataSource
和delegate
问题,那么它将修复下面的颜色错误。
。H:
#import <UIKit/UIKit.h>
#import "ProductContentViewController.h"
#import "LiquidFloatingActionButton-swift.h"
@interface SetScreenViewController : UIViewController
<UIPageViewControllerDataSource, LiquidFloatingActionButtonDelegate, LiquidFloatingActionButtonDataSource>
...
@end
.M:
#import "LiquidFloatingActionButton-Swift.h"
LiquidFloatingActionButton *floatingActionButton;
NSMutableArray *liquidCells;
bool *closeFloatingButtons;
NSString *videoToWatchURL;
- (void)addLiquidButton{
//Grabs the coordinates bottom right of the screen
float X_Co = self.view.frame.size.width - 50;
float Y_Co = self.view.frame.size.height - 50;
//i subtract 10 so the button will be placed a little bit out
X_Co = X_Co - 10;
Y_Co = Y_Co - 10;
//I create each cell and set the image for each button
liquidCells = [[NSMutableArray alloc] init];
[liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
[liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
[liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
[liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
[liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
[liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
//Sets the floating button at the loaction provided
floatingActionButton = [[LiquidFloatingActionButton alloc] initWithFrame:CGRectMake(X_Co, Y_Co, 50, 50)];
floatingActionButton.dataSource = self;//Error here
floatingActionButton.delegate = self;//and here.
//I set the color of the floating button
floatingActionButton.color = [self colorWithHexString:@"01b8eb"];
//Enables the user interaction fuction to true so itll open or close
floatingActionButton.userInteractionEnabled = YES;
//Adds the flaoting button to the view
[self.view addSubview:floatingActionButton];
}
-(NSInteger)numberOfCells:(LiquidFloatingActionButton *)liquidFloatingActionButton{
return liquidCells.count;
}
-(LiquidFloatingCell *)cellForIndex:(NSInteger)index{
return [liquidCells objectAtIndex:index];
}
PodFile:
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
target 'Whats New' do
# Uncomment this line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for Whats New
target 'Whats NewTests' do
inherit! :search_paths
# Pods for testing
end
target 'Whats NewUITests' do
inherit! :search_paths
# Pods for testing
end
pod 'CRToast', '~> 0.0.7'
pod "LiquidFloatingActionButton"
pod 'DGActivityIndicatorView'
pod 'M13ProgressSuite'
pod 'SDWebImage', '~>3.8'
pod 'FSCalendar'
use_frameworks!
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.0'
end
end
end
end
更新下面
我最终使用this项目,因为原来的GitHub没有得到修复,感谢所有的帮助,如果有人想出这个,请让大家知道!
如果快速的@protocols
没有用@objc
定义那么你就无法在objective-c中访问它们,
e.g
@objc public protocol xxxxxxxxxxxDelegate {
func functionOne()
func functionSecond()
}
我认为问题是你的Objective-C代码不知道open var
的dataSource
和delegate
的LiquidFloatingButton
Swift代码。
为了使它们知道,需要一个Obj-c头文件,将这些Swift信息公开给你的Obj-C代码。这在Apple文档“Swift and Objective-C in the Same Project”中有详细描述,在您的情况下,特别是在“导入外部框架”部分中。它说:
您可以使用以下语法将框架导入到不同目标中的任何Objective-C .m文件中:
@import FrameworkName;
您是否为项目添加了桥接头?要在Objective-C项目中使用Swift代码,您需要一个桥接头。这是做什么的,
1.创建一个新的.swift文件
2.将出现一个弹出窗口并询问“您是否要配置Objective-C桥接标题”。选择是。
3.单击您的Xcode Project文件,单击Build Settings
4.找到搜索栏并搜索“定义模块”。
5.将值更改为是。
6.在App委托中,添加以下内容:#import“YourProjectName-swift.h”
每当您想要使用Swift文件时,您必须添加以下行:
#Import "YourProjectName-swift.h"
我也在Swift 3项目中使用LiquidFloatingActionButton
。请注意,git上的项目使用SnapKit
作为Swift 2.3,我也必须处理它,但这不在这里。我的解决方案不是使用@objc public protocol
,而是更改协议声明如下:
public protocol LiquidFloatingActionButtonDataSource {
func numberOfCells(_ liquidFloatingActionButton: LiquidFloatingActionButton) -> Int
func cellForIndex(_ index: Int) -> LiquidFloatingCell
}
并且:
public protocol LiquidFloatingActionButtonDelegate {
// selected method
func liquidFloatingActionButton(_ liquidFloatingActionButton: LiquidFloatingActionButton, didSelectItemAtIndex index: Int)
}
只有我能在我的Swift 3项目中成功使用委托。