我有一组视图控制器,它们有一个菜单栏按钮.我为那些viewControllers创建了一个协议来采用.此外,我已经扩展了协议以添加默认功能.
我的协议看起来像,
protocol CenterViewControllerProtocol: class {
var containerDelegate: ContainerViewControllerProtocol? { get set }
func setupMenuBarButton()
}
Run Code Online (Sandbox Code Playgroud)
并且,扩展看起来像这样,
extension CenterViewControllerProtocol where Self: UIViewController {
func setupMenuBarButton() {
let barButton = UIBarButtonItem(title: "Menu", style: .Done, target: self, action: "menuTapped")
navigationItem.leftBarButtonItem = barButton
}
func menuTapped() {
containerDelegate?.toggleSideMenu()
}
}
Run Code Online (Sandbox Code Playgroud)
我的viewController采用协议 -
class MapViewController: UIViewController, CenterViewControllerProtocol {
weak var containerDelegate: ContainerViewControllerProtocol?
override func viewDidLoad() {
super.viewDidLoad()
setupMenuBarButton()
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了很好的显示按钮,但是当我点击它时,应用程序崩溃了
[AppName.MapViewController menuTapped]: unrecognized selector sent to instance 0x7fb8fb6ae650
Run Code Online (Sandbox Code Playgroud)
如果我在ViewController中实现该方法,它可以正常工作.但我要复制符合协议的所有viewControllers中的代码.
我在这里做错了什么?提前致谢.
我正在使用默认的UIImagePickerController,并将allowsEditing设置为YES来拍摄照片.当用户移动并缩放照片时,操作系统会要求访问"照片".如果用户拒绝访问,应用程序将崩溃.
- (void)openCamera
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.editing = YES;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
和UIImagePickerControllerDelegate方法一样
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage];
if (!image) {
image = info[UIImagePickerControllerOriginalImage];
}
self.profileImage = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
崩溃消息:
*** This application is not allowed to access Photo data.
Run Code Online (Sandbox Code Playgroud)
我想知道为什么它应该首先要求访问照片.有什么想法吗?
我正在尝试使用Diesel和 PostgreSQL执行插入多个列。
这是添加新的插入功能Project-
pub fn insert(project: NewProject, program_id: i32, conn: &PgConnection) -> bool {
use schema::projects::dsl::*;
use schema::projects::dsl::{title as t};
use schema::projects::dsl::{program_id as prog_id};
let NewProject {
title
} = project;
diesel::insert_into(projects)
.values((t.eq(title), prog_id.eq(program_id)))
.execute(conn)
.is_ok()
}
Run Code Online (Sandbox Code Playgroud)
而Project与NewProject
#[derive(Queryable, Serialize, Debug, Clone)]
pub struct Project {
pub id: i32,
pub title: String,
pub program_id: i32,
pub is_archived: bool
}
#[derive(Serialize, Deserialize, Insertable)]
#[table_name = "projects"]
pub struct NewProject {
pub title: String
} …Run Code Online (Sandbox Code Playgroud) 我有一个类型的数组 [String]
let names = ["Joffrey", "Cersei", "Mountain", "Hound"]
Run Code Online (Sandbox Code Playgroud)
我有一个带有[Any]类型数组的函数.
func printItems(items: [Any]){
for item in items {
print(item)
}
}
Run Code Online (Sandbox Code Playgroud)
现在当我用namesas参数调用函数时,
printItems(names)
Run Code Online (Sandbox Code Playgroud)
我收到错误无法使用类型为'([String])'的参数列表调用'printItems'.
Any只是一个typealias所有类型隐式符合的协议.
思考?