Swift annotation error for loop with array and UIView

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

I think I did a good job converting and searching for answers as I am just starting out with Obj-C and Swift. But now I am stuck with an error, I can not find a solution.

ExpandableNavigation.swift

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 } } 

Error expanding Navigation.swift

Error: Type annotation does not match contextual type 'AnyObject'

This error occurs both in the init () methods and in the expand () method, which says for view: UIView in self.menuItems { I tried to make an array of UIView [], but it causes an else error where.

ViewController.swift - for reference

 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) } 
+7
swift nsarray
source share
1 answer

Swift will not do any hidden castings for you. It will not pass AnyObject (which is the type that NSArray stores) to 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; } } 

This loop goes through an array of menu items with AnyObject and checks if obj is a representation before making it like.

+19
source share

All Articles