Swift for 循环中的注释错误,带有数组和 UIView [英] Annotation error in Swift for loop with array and UIView

查看:43
本文介绍了Swift for 循环中的注释错误,带有数组和 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为将一个小型 Obj-C 库转换为 Swift 将是一个很好的学习实验.由于我最终想在应用程序中使用这个库,我在这里找到了路径应用程序菜单示例:https://github.com/yourabi/PathMenuExample

I thought it would be a great learning experiment to take a small Obj-C library and convert it to Swift. Since I eventually want to use this library in an app, I found the Path app menu example here: https://github.com/yourabi/PathMenuExample

我认为我在转换和搜索答案方面做得还不错,因为我刚开始使用 Obj-C 和 Swift.但是现在我遇到了一个找不到解决方案的错误.

I think I've done an okay job of converting and searching for answers as I'm just starting out with Obj-C and Swift. But now I'm stuck with an error I can't find a solution for.

import Foundation
import UIKit;

class ExpandableNavigation: NSObject {

    var mainButton: UIButton;
    var menuItems: NSArray;
    var radius: CGFloat;
    var speed: CGFloat;
    var bounce: CGFloat;
    var bounceSpeed: CGFloat;
    var expanded: Bool;
    var transition: Bool;


    init(menuItems: NSArray, mainButton: UIButton, radius: CGFloat) {
            self.menuItems = menuItems;
            self.mainButton = mainButton;
            self.radius = radius;
            self.speed = 0.15;
            self.bounce = 0.225;
            self.bounceSpeed = 0.1;
            expanded = false;
            transition = false;

            super.init()

            if self.mainButton != nil {
                for view: UIView in self.menuItems {
                    view.center = self.mainButton.center;
                }

                self.mainButton.addTarget(self, action:"press", forControlEvents: UIControlEvents.TouchUpInside);
            }
    }

    func expand() -> Void {
        transition = true;

        UIView.animateWithDuration(0.15, animations: {
            self.mainButton.transform = CGAffineTransformMakeRotation(45.0 * M_PI/180)
        })

        for view: UIView in self.menuItems {
            var index: Int? = findIndex(self.menuItems, valueToFind: view);
            var oneOverCount: Float;
            if self.menuItems.count <= 1 {
                oneOverCount = 1.0;
            } else {
                oneOverCount = (1.0 / Float(self.menuItems.count - 1));
            }
            var indexOverCount: CGFloat = CGFloat(index!) * CGFloat(oneOverCount);
            var rad: CGFloat = (1.0 - CGFloat(indexOverCount)) * 90.0 * CGFloat(M_PI) / 180.0;
            var rotation: CGAffineTransform = CGAffineTransformMakeRotation(rad);
            var x: CGFloat = (self.radius + self.bounce * self.radius) * rotation.a;
            var y: CGFloat = (self.radius + self.bounce * self.radius) * rotation.c;
            var center: CGPoint = CGPointMake(view.center.x + x, view.center.y + y);
            UIView.animateWithDuration(self.speed,
                delay: self.speed * indexOverCount,
                options: UIViewAnimationOptions.CurveEaseIn,
                animations: {
                    view.center = center;
                },
                completion: {(finished: Bool) in
                    UIView.animateWithDuration(self.bounceSpeed,
                        animations: {
                            var x: CGFloat = self.bounce * self.radius * rotation.a;
                            var y: CGFloat = self.bounce * self.radius * rotation.c;
                            var center: CGPoint = CGPointMake(view.center.x + x, view.center.y + y);
                            view.center = center;
                        });
                    if view == (self.menuItems.endIndex - 1) {
                        self.expanded = true;
                        self.transition = false;
                    }
                })

        }
    }

    func collapse() -> Void {
        transition = true;

    }

    @IBAction func press(sender: AnyObject) {
        if !self.transition {
            if !self.expanded {
                self.expand();
            } else {
                self.collapse();
            }
        }
    }

    func findIndex<T: Equatable>(array: T[], valueToFind: T) -> Int? {
        for (index, value) in enumerate(array) {
            if value == valueToFind {
                return index
            }
        }
        return nil
    }
}

ExpandableNavigation.swift 出错

错误:类型注释与上下文类型AnyObject"不匹配

这个错误发生在 init() 和 expand() 方法中,它说for view: UIView in self.menuItems {我尝试制作数组 UIView[] 但这会导致其他地方出错.

This error occurs both in the init() and the expand() methods where it says for view: UIView in self.menuItems { I tried making the array UIView[] but that causes errors else where.

class ViewController: UIViewController {
@IBOutlet var expandNavMainButton: UIButton;
@IBOutlet var expandNavCheckInButton: UIButton;
@IBOutlet var expandNavReviewButton: UIButton;
@IBOutlet var expandNavPhotoButton: UIButton;
@IBOutlet var expandNavStatusButton: UIButton;
var expandableNavigation: ExpandableNavigation;

init(coder aDecoder: NSCoder!) {
    var buttons: NSArray = [expandNavCheckInButton, expandNavReviewButton, expandNavPhotoButton, expandNavStatusButton];
    var myRadius: CGFloat = 120.0
    self.expandableNavigation = ExpandableNavigation(menuItems: buttons, mainButton: self.expandNavMainButton, radius: myRadius);
    super.init(coder: aDecoder)
}

推荐答案

为了安全起见,Swift 不会为您进行任何隐式转换.它不会将 AnyObject(NSArray 存储的类型)转换为没有显式转换的 UIView.您应该执行以下操作:

Swift will not do any implicit casting for you for safety. It will not cast AnyObject (which is the type that NSArray stores) to a UIView without an explicit cast. You should do the following:

for obj : AnyObject in menuItems {
    if let view = obj as? UIView {
        view.center = self.mainButton.center;
    }
}

这会使用 AnyObject 循环遍历菜单项数组,并在将其转换为一个视图之前检查 obj 是否为视图.

This loops through the array of menu items with an AnyObject and the checks if obj is a view before casting it as one.

这篇关于Swift for 循环中的注释错误,带有数组和 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆