所以我将Xcode和GitHub项目代码升级到swift 3.0(我的项目在Obj C中,我在GitHub中使用的一些pod都在Swift中)。我收到了一些错误,现在我被困在这些错误上了。出于某种原因,我的floatingActionButton(git here)的dataSource和delegate现在不起作用。我尝试以编程方式和故事板设置dataSource
和delegate
,但它没有用。
错误:
Property' dataSource'在' LiquidFloatingActionButton *'
类型的对象上找不到财产'代表'在' 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没有被修复,感谢所有的帮助,如果有人想出来,请让大家知道!
答案 0 :(得分:3)
如果未使用@protocols
定义swift @objc
,则无法在objective-c中访问它们,
e.g
@objc public protocol xxxxxxxxxxxDelegate {
func functionOne()
func functionSecond()
}
答案 1 :(得分:1)
我认为问题是您的Objective-C代码不知道open var
Swift代码的“dataSource
”delegate
和LiquidFloatingButton
。
为了使它们知道,需要一个Obj-c头文件,将这些Swift信息公开给你的Obj-C代码。这在Apple文档“Swift and Objective-C in the Same Project”中有详细描述,在您的情况下,特别是在“导入外部框架”部分中。它说:
您可以使用以下语法将框架导入到不同目标中的任何Objective-C .m文件中:
@import FrameworkName;
答案 2 :(得分:1)
您是否在项目中添加了桥接标题?要在Objective-C项目中使用Swift代码,您需要一个桥接头。这是做什么的,
1.创建一个新的.swift文件
2.将出现一个弹出窗口并询问&#34;您是否要配置Objective-C桥接标题&#34; 。选择是。
3.点击您的Xcode Project文件,点击构建设置
4.找到搜索栏并搜索定义模块。
5.将值更改为是。
6.在App委托中,添加以下内容: #import&#34; YourProjectName-swift.h&#34;
每当您想要使用Swift文件时,您必须添加以下行:
#Import "YourProjectName-swift.h"
答案 3 :(得分:0)
我还在 Swift 3 项目中使用LiquidFloatingActionButton
。请注意,git上的项目对{strong> Swift 2.3 使用SnapKit
,我也必须处理,但这不在此范围内。我的解决方案不是使用@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 项目中成功使用委托。