nsarray – 带有数组和UIView的Swift循环中的注释错误

前端之家收集整理的这篇文章主要介绍了nsarray – 带有数组和UIView的Swift循环中的注释错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我认为采用一个小的Obj-C库并将其转换为 Swift是一个很好的学习实验.由于我最终想在应用程序中使用此库,因此我在此处找到了Path app菜单示例: https://github.com/yourabi/PathMenuExample

我认为我已经做好了转换和搜索答案的工作,因为我刚刚开始使用Obj-C和Swift.但现在我遇到了一个我无法找到解决方案的错误.

ExpandableNavigation.swift

  1. import Foundation
  2. import UIKit;
  3.  
  4. class ExpandableNavigation: NSObject {
  5.  
  6. var mainButton: UIButton;
  7. var menuItems: NSArray;
  8. var radius: CGFloat;
  9. var speed: CGFloat;
  10. var bounce: CGFloat;
  11. var bounceSpeed: CGFloat;
  12. var expanded: Bool;
  13. var transition: Bool;
  14.  
  15.  
  16. init(menuItems: NSArray,mainButton: UIButton,radius: CGFloat) {
  17. self.menuItems = menuItems;
  18. self.mainButton = mainButton;
  19. self.radius = radius;
  20. self.speed = 0.15;
  21. self.bounce = 0.225;
  22. self.bounceSpeed = 0.1;
  23. expanded = false;
  24. transition = false;
  25.  
  26. super.init()
  27.  
  28. if self.mainButton != nil {
  29. for view: UIView in self.menuItems {
  30. view.center = self.mainButton.center;
  31. }
  32.  
  33. self.mainButton.addTarget(self,action:"press",forControlEvents: UIControlEvents.TouchUpInside);
  34. }
  35. }
  36.  
  37. func expand() -> Void {
  38. transition = true;
  39.  
  40. UIView.animateWithDuration(0.15,animations: {
  41. self.mainButton.transform = CGAffineTransformMakeRotation(45.0 * M_PI/180)
  42. })
  43.  
  44. for view: UIView in self.menuItems {
  45. var index: Int? = findIndex(self.menuItems,valueToFind: view);
  46. var oneOverCount: Float;
  47. if self.menuItems.count <= 1 {
  48. oneOverCount = 1.0;
  49. } else {
  50. oneOverCount = (1.0 / Float(self.menuItems.count - 1));
  51. }
  52. var indexOverCount: CGFloat = CGFloat(index!) * CGFloat(oneOverCount);
  53. var rad: CGFloat = (1.0 - CGFloat(indexOverCount)) * 90.0 * CGFloat(M_PI) / 180.0;
  54. var rotation: CGAffineTransform = CGAffineTransformMakeRotation(rad);
  55. var x: CGFloat = (self.radius + self.bounce * self.radius) * rotation.a;
  56. var y: CGFloat = (self.radius + self.bounce * self.radius) * rotation.c;
  57. var center: CGPoint = CGPointMake(view.center.x + x,view.center.y + y);
  58. UIView.animateWithDuration(self.speed,delay: self.speed * indexOverCount,options: UIViewAnimationOptions.CurveEaseIn,animations: {
  59. view.center = center;
  60. },completion: {(finished: Bool) in
  61. UIView.animateWithDuration(self.bounceSpeed,animations: {
  62. var x: CGFloat = self.bounce * self.radius * rotation.a;
  63. var y: CGFloat = self.bounce * self.radius * rotation.c;
  64. var center: CGPoint = CGPointMake(view.center.x + x,view.center.y + y);
  65. view.center = center;
  66. });
  67. if view == (self.menuItems.endIndex - 1) {
  68. self.expanded = true;
  69. self.transition = false;
  70. }
  71. })
  72.  
  73. }
  74. }
  75.  
  76. func collapse() -> Void {
  77. transition = true;
  78.  
  79. }
  80.  
  81. @IBAction func press(sender: AnyObject) {
  82. if !self.transition {
  83. if !self.expanded {
  84. self.expand();
  85. } else {
  86. self.collapse();
  87. }
  88. }
  89. }
  90.  
  91. func findIndex<T: Equatable>(array: T[],valueToFind: T) -> Int? {
  92. for (index,value) in enumerate(array) {
  93. if value == valueToFind {
  94. return index
  95. }
  96. }
  97. return nil
  98. }
  99. }

ExpandableNavigation.swift出错

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

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

ViewController.swift – 供参考

  1. class ViewController: UIViewController {
  2. @IBOutlet var expandNavMainButton: UIButton;
  3. @IBOutlet var expandNavCheckInButton: UIButton;
  4. @IBOutlet var expandNavReviewButton: UIButton;
  5. @IBOutlet var expandNavPhotoButton: UIButton;
  6. @IBOutlet var expandNavStatusButton: UIButton;
  7. var expandableNavigation: ExpandableNavigation;
  8.  
  9. init(coder aDecoder: NSCoder!) {
  10. var buttons: NSArray = [expandNavCheckInButton,expandNavReviewButton,expandNavPhotoButton,expandNavStatusButton];
  11. var myRadius: CGFloat = 120.0
  12. self.expandableNavigation = ExpandableNavigation(menuItems: buttons,mainButton: self.expandNavMainButton,radius: myRadius);
  13. super.init(coder: aDecoder)
  14. }
为了安全起见,Swift不会为你进行任何隐式的施法.它不会在没有显式强制转换的情况下将AnyObject(NSArray存储的类型)强制转换为UIView.您应该执行以下操作:
  1. for obj : AnyObject in menuItems {
  2. if let view = obj as? UIView {
  3. view.center = self.mainButton.center;
  4. }
  5. }

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

猜你在找的Swift相关文章