小编iOS*_*eek的帖子

将对象数组映射到Swift中的Dictionary

我有一个Person对象的数组:

class Person {
   let name:String
   let position:Int
}
Run Code Online (Sandbox Code Playgroud)

而数组是:

let myArray = [p1,p1,p3]
Run Code Online (Sandbox Code Playgroud)

我想映射myArray[position:name]经典解决方案的字典是:

var myDictionary =  [Int:String]()

for person in myArray {
    myDictionary[person.position] = person.name
}
Run Code Online (Sandbox Code Playgroud)

Swift是否有任何优雅的方式来实现功能性方法map,flatMap......或其他现代Swift风格

arrays dictionary ios swift

60
推荐指数
5
解决办法
4万
查看次数

更新UITableView时,新建可见行的缺失单元格崩溃

我删除一行时遇到崩溃.

// Updating my data model 
....
// apply the updates
self.tableView.beginUpdates()
self.tableView.deleteRows(at: indexPathsToDelete, with: .automatic)
self.tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)

重现的步骤 - 添加行 - 删除行,特别是确保当前屏幕外有一些行(当删除成功时将显示在屏幕中 - 重复直到崩溃发生

它并不总是发生,所以我最好的猜测是它只会在它试图加载的单元格被回收时发生

这是使用Xcode 8.0的10.0模拟器

  *** Assertion failure in -[UITableView _updateWithItems:updateSupport:],
 /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:3149
 Missing cell for newly visible row 2
 (null)
Run Code Online (Sandbox Code Playgroud)

cellForRowAt的代码

if isMultipe{
   let cell =  tableView.dequeueReusableCell(withIdentifier: DetailsTableViewCell.defaultIdentifier, for: indexPath) as! DetailsTableViewCell
return cell
    } else {
let cell =  tableView.dequeueReusableCell(withIdentifier: DetailsMultipleTableViewCell.defaultIdentifier, for: indexPath) as! DetailsMultipleTableViewCell
   return cell
}
Run Code Online (Sandbox Code Playgroud)

这里报告了同样的错误:https://forums.developer.apple.com/thread/49676

uitableview ios swift

18
推荐指数
2
解决办法
3926
查看次数

使用Swift 2将String转换为NSDate

我正在尝试将字符串转换为NSDate此处是我的代码

     let strDate = "2015-11-01T00:00:00Z" // "2015-10-06T15:42:34Z"
     let dateFormatter = NSDateFormatter()
     dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ssZ"
    print ( dateFormatter.dateFromString( strDate ) )
Run Code Online (Sandbox Code Playgroud)

我不断得到nil结果

nsdateformatter ios swift2

15
推荐指数
1
解决办法
2万
查看次数

在Xcode中当前活动选项卡旁边打开新选项卡

当您在Xcode中点击CMD+ T它将打开新选项卡但在最后一个位置时,如何更改此行为以便能够在当前活动选项卡旁边打开新选项卡

macos xcode

13
推荐指数
2
解决办法
1万
查看次数

通用全局优雅方式将条形按钮项添加到项目的任何UIViewController

通常我们UIBarButtonItem在项目中有一个可以在项目中使用的预定义集合,多次像左侧菜单按钮一样打开侧面菜单,它可以在不同的UIViewControllers中使用,也可以使用关闭按钮来关闭呈现的视图控制器.

经典的方法是根据需要添加这些按钮,但这会引入代码重复,我们都希望避免这种情况.

我提出了一种方法,但它远非完美:

enum BarButtonItemType {
    case menu, close, notification
}

enum BarButtonItemPosition{
    case right, left
}

extension UIViewController {

    func add(barButtons:[BarButtonItemType], position: BarButtonItemPosition) {

        let barButtonItems = barButtons.map { rightBarButtonType -> UIBarButtonItem in
            switch rightBarButtonType {
            case .menu:
                return UIBarButtonItem(image: UIImage(named:"menu"),
                    style: .plain,
                    target: self,
                    action: #selector(presentLeftMenu(_:)))
            case .notification:
                return UIBarButtonItem(image: UIImage(named:"notification"),
                style: .plain,
                target: self,
                action: #selector(showNotification(_:)))
            case .close:
                return UIBarButtonItem(image: UIImage(named:"close"),
                    style: .plain,
                    target: self,
                    action: #selector(dismissController(_:)))
            }
        }

        switch position {
        case …
Run Code Online (Sandbox Code Playgroud)

uinavigationcontroller uibarbuttonitem uinavigationitem ios swift

12
推荐指数
1
解决办法
671
查看次数

Swift中Enum的默认值

我有一个enum:

public enum PersonType:String {

 case Cool                       = "cool"
 case Nice                       = "rude"
 case SoLazy                     = "so-lazy"

 public var description: String {
    switch self {
    case .Cool:
        return "Cool person"
    case .Nice:
        return "Nice person"
    case .SoLazy:
        return "its so lazy person"
    }
}


 public var typeImage: String {
    switch self {
    case .Cool:
        return "cool.png"
    case .Nice:
        return "img_nice.png"
    case .Solazy:
        return "lazy.png"
    }
   }  

}
Run Code Online (Sandbox Code Playgroud)

问题我不知道所有的人类型键,所以我需要处理类型为人的默认情况,并给它描述将是它的关键像"so-lazy"和默认图像.

假设我从Web服务获得此结果:

[
    {
        name: "john",
        key: "cool"
    },
    {
        name: …
Run Code Online (Sandbox Code Playgroud)

enums ios data-structures swift swift2

11
推荐指数
6
解决办法
2万
查看次数

带有swift的依赖注入,带有两个UIViewControllers的依赖图,没有公共父级

我们如何在不使用框架的情况下应用依赖注入,当我们有两个层次结构非常深的UIViewController并且它们都需要相同的依赖关系来保存状态时,那两个UIViewControllers它们没有共同的父代.

例:

VC1 - > VC2 - > VC3 - > VC4

VC5 - > VC6 - > VC7 - > VC8

让我们看看他们都需要VC4和VC8 UserService来保存当前用户.

请注意,我们要避免使用Singleton.

是否有一种优雅的方式来处理这种DI情况?

经过一番研究,我发现,有些提Abstract Factory,Context interfaces,Builder,strategy pattern

但我找不到如何在iOS上应用它的示例

dependency-injection inversion-of-control factory-pattern ios swift

8
推荐指数
1
解决办法
573
查看次数

UIViewController的默认自定义转换

我正在开发一个库,我想在两个视图控制器之间提供默认的自定义转换,用户也可以提供自己的实现,我想到的第一个想法是覆盖UIViewController和实现UIViewControllerTransitioningDelegate,然后用户可以继承我的CustomTransitionViewController是它最好的方法吗?任何限制?是否有一种更优雅的方式只使用协议,例如默认实现?

import UIKit

class CustomTransitionViewController: UIViewController, UIViewControllerTransitioningDelegate {

  required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
      self.transitioningDelegate = self
  }

  override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil:Bundle?)   {
      super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
      self.transitioningDelegate = self
  }

  func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
      return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8)
  }

  func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
      return FadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8)
  }
}
Run Code Online (Sandbox Code Playgroud)

uiviewcontroller ios swift swift-protocols

7
推荐指数
2
解决办法
605
查看次数

在 Swift 4 中从“POPSpringAnimation”迁移到本机 iOS 框架

我正在开发一个旧项目,想要摆脱POP 框架,我确信任何动画都可以使用本机 iOS 框架来完成。

这是旧代码:

POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
springAnimation.toValue = [NSValue valueWithCGRect:rect];
springAnimation.velocity = [NSValue valueWithCGRect:CGRectMake(springVelocity, springVelocity, 0, 0)];
springAnimation.springBounciness = springBounciness;
springAnimation.springSpeed = springSpeed;
[springAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
    if (finished) {
         // cool code here
    }
}];

[self.selectedViewController.view pop_addAnimation:springAnimation forKey:@"springAnimation"];
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

[UIView animateWithDuration:1.0
                      delay:0
     usingSpringWithDamping:springBounciness
      initialSpringVelocity:springVelocity
                    options:UIViewAnimationOptionCurveEaseInOut animations:^{
                        self.selectedViewController.view.frame = rect;
} completion:^(BOOL finished) {
    // cool code here
}];
Run Code Online (Sandbox Code Playgroud)

但我没有得到相同的结果,并且出现了一些问题:

  1. springBounciness在流行音乐中相当于usingSpringWithDamping
  2. springSpeed动画中相当于什么UIView
  3. 持续时间怎么样? 的持续时间是多少POPSpringAnimation? …

core-animation objective-c uiviewanimation ios facebook-pop

6
推荐指数
1
解决办法
580
查看次数

String to Double转换在Swift中失去了精度

我想将一个字符串转换为double并保持相同的值:

let myStr = "2.40"
let numberFormatter = NSNumberFormatter()
numberFormatter.locale = NSLocale(localeIdentifier: "fr_FR")
let myDouble = numberFormatter.numberFromString(myStr)?.doubleValue ?? 0.0
Run Code Online (Sandbox Code Playgroud)

myDouble现在

Double? 2.3999999999999999
Run Code Online (Sandbox Code Playgroud)

那么如何将"2.40"转换为精确到2.40为双?

更新:

即使转换后舍入也似乎不起作用

我不想打印,我想计算,重要的是数字应该是正确的,这是货币计算和费率

floating-point double nsnumberformatter ios swift2.2

5
推荐指数
1
解决办法
1768
查看次数