我认为我已经做好了转换和搜索答案的工作,因为我刚刚开始使用Obj-C和Swift.但现在我遇到了一个我无法找到解决方案的错误.
ExpandableNavigation.swift
import Foundationimport 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 [],但这导致其他错误.
VIEwController.swift – 供参考
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.您应该执行以下 *** 作:
for obj : AnyObject in menuItems { if let vIEw = obj as? UIVIEw { vIEw.center = self.mainbutton.center; }}
这将使用AnyObject循环遍历菜单项数组,并在将obj作为一个视图投射之前检查obj是否为视图.
总结以上是内存溢出为你收集整理的nsarray – 带有数组和UIView的Swift循环中的注释错误全部内容,希望文章能够帮你解决nsarray – 带有数组和UIView的Swift循环中的注释错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)