小编non*_*tic的帖子

更新Swift 3的闭包 - @escaping

我已经将我的代码更新为Xcode 8.0 beta 6,但我似乎陷入了关于新的非转义关闭默认值的问题.在下面的代码中,Xcode建议在下面代码的第一行@escaping前面添加completion:,但是仍然不会编译并进入圆圈.*

(编辑:事实上,@scaping应该在之后 添加completion:,正如Xcode建议的那样.警报可能仍然显示,但清理和编译将删除它.)*如何重新编写/修复此代码以在更新的Swift 3中工作?我看过新手册,但找不到合适的代码示例.

func doSomething(withParameter parameter: Int, completion: () -> ()) {
    // Does something

    callSomeOtherFunc(withCompletion: completion)
  }

// Calling the method and execute closure 
doSomething(withParameter: 2) {
  // do things in closure
}
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢!

xcode closures escaping swift3

75
推荐指数
2
解决办法
5万
查看次数

停止在for循环中使用playAtTime()设置多个AVAudioPlayers

当选择一行时,以下代码设置一组AVAudioPlayers以在特定日期播放(在这种情况下,50个玩家以1秒的间隔播放).由于我希望在再次触摸时重新启动整个过程,因此需要在for循环中中断设置,因为设置播放器需要几秒钟.

除此之外,每个播放器在使用audioDidFinishPlaying委托方法完成播放后被删除AVAudioPlayerDelegate.我没有在代码中包含它,因为它与问题无关.

我已经尝试在for循环中使用一个标志来检查是否允许设置,但这不起作用.

var players: [AVAudioPlayer] = []
var loadError: NSError?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

  // Removes the players that have been setup already.
  players.removeAll()

  // The for loop from the previous row selection should be stopped here.
  for i in 0..<50 {
    do {
      let player = try AVAudioPlayer(contentsOfURL: soundUrls[i])
      players += [player]

      // The process of setting these up takes a few seconds, I need to …
Run Code Online (Sandbox Code Playgroud)

for-loop avaudioplayer ios swift

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

Swift 3警告:检查选项时使用的"String"类型的非可选表达式

我正在更新一个项目到Swift 3并遇到以下警告,我似乎无法解决.

fileprivate var filteredTitlesList: [String] = []

if let filteredTitle: String = filteredTitlesList[indexPath.row] as String { // 'Non-optional expression of type 'String' used in a check for optionals'

  // Do something

}
Run Code Online (Sandbox Code Playgroud)

这里类似问题的答案对我没有帮助:在检查选项时使用的"AnyObject"类型的非可选表达式

非常感谢!

string xcode swift

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

iOS 13 黑暗模式:traitCollectionDidChange 只在第一次调用

我的应用程序使用自定义颜色主题,但 iOS13 用户可以选择以下深色模式。

我以为我可以简单地更新我的颜色,ViewController's traitCollectionDidChange()但由于某种原因,这个函数只在用户第一次更改 iOS 设置中的界面样式时调用。虽然这对大多数用户来说可能已经足够了,但理想情况下,traitCollectionDidChange()每次用户更改其 iOS 设置时都应调用。

只是非常基本的:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  super.traitCollectionDidChange(previousTraitCollection)
  print(#function)

  guard traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle else { return }

  NotificationCenter.default.post(name: NSNotification.Name(Keys.Notifications.updateColorTheme), object: nil)
}
Run Code Online (Sandbox Code Playgroud)

我启动我的应用程序,离开它,在 iOS 设置中更改外观:下次我打开应用程序(打开,不启动)时,上述函数被调用,应用程序更新颜色。现在,当我重复相同的过程时,不再调用该函数。

collections traits ios swift

7
推荐指数
1
解决办法
2901
查看次数

iOS:本地化多行字符串文字

从最近的快速版本开始,可以使用多行字符串文字来轻松格式化多行字符串的外观。

我正在寻找一种方法来本地化这样的字符串。

这是用户可以发送的预配置邮件文本的示例:

mailComposerVC.setMessageBody("""
                              Hi,

                               I would like to share the following feedback:

                              """,
                              isHTML: false)
Run Code Online (Sandbox Code Playgroud)

似乎没有办法将其正确转换为可本地化的.strings文件。

作为解决方法,我想出了一种解决方案,可以分别定位消息的每个部分并使用插值:

let localisedGreeting = NSLocalizedString("Hi", comment: "")
let localisedMessage = NSLocalizedString("I would like to share the following feedback: ", comment: "")
    mailComposerVC.setMessageBody("""
                                  \(localisedGreeting),

                                   \(localisedMessage)

                                  """,
                                  isHTML: false)
Run Code Online (Sandbox Code Playgroud)

.strings文件如下所示:

"Hi" = "Hallo";
"I would like to share the following feedback: " = "ich möchte folgendes Feedback geben: ";
Run Code Online (Sandbox Code Playgroud)

有没有更好/更简洁的方法可以做到这一点?

string localization ios swift

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

警告:“无法获取iPhone11,6的traitsetID”(Assets.xcassets)

在XS Max物理设备上运行Xcode 10应用程序时,我总是收到此警告:

无法获取iPhone11,6的traitsetID(Assets.xcassets)

任何想法,这是关于什么以及如何解决?

xcode ios

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

更改音频会话类别后 MPRemoteCommandCenter 不可用

我的应用程序可以选择允许其声音与其他应用程序混合。据苹果公司称,MPRemoteCommandCenter 仅在应用程序不允许混合时可用。

在我的应用程序中,当用户点击按钮更改 mixWithOthers 设置时,音频会话也会相应设置。

但是,即使用户切换回不再允许混合,MPRemoteCommandCenter 也不会显示在锁定屏幕中,直到应用程序从缓存中删除(向上滑动)并再次启动。

有没有办法无需重新启动应用程序即可实现所需的行为?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
  UIApplication.shared.beginReceivingRemoteControlEvents()
}

var isMixWithOthersAllowed: Bool

func startProcess() {
  setAudioSession {
   setupMediaControls()
  }
}

func setAudioSession(completion: @escaping () -> Void) {
  let audioSession = AVAudioSession.sharedInstance()

  do {

    if isMixWithOthersAllowed {
      try audioSession.setCategory(.playback, options: [.mixWithOthers])
      /* The remote command center will not be available when mixing with others, 
        as stated by Apple in the docs. */
    } else {
      try …
Run Code Online (Sandbox Code Playgroud)

xcode avaudiosession swift mpremotecommandcenter avaudiosessioncategory

5
推荐指数
0
解决办法
711
查看次数

Swift 2迁移错误:不再能在SKSpriteNode的指定初始化中传入nil

试图将我的项目迁移到Swift 2.

我遇到了很多编译器错误,其中大部分都很容易修复,因为它们只是语法更改.

但是,我不知道处理此错误的最佳方法:

当调用从我的子类的指定初始化器SKSpriteNode我得到错误信息:"类型'的UIColor’不符合协议'NilLiteralConvertible’"传递nilcolor:

import SpriteKit

class MyClass: SKSpriteNode {

  init() {
    let texture = SKTexture(imageNamed: "MyTexture.png")
    super.init(texture: texture, color: nil, size: texture.size())
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以通过传入解决这个问题,UIColor.clearColor()但我觉得这不是处理这个问题的正确方法.

任何建议都非常感激,而且,这里发生了什么变化(当然,解释的链接就足够了)?

sprite-kit skspritenode xcode7 swift2

4
推荐指数
1
解决办法
880
查看次数

更改UISearchBar中UItextField中图标的颜色

我正在尝试在搜索控制器中自定义搜索栏的外观.

设置背景和文本颜色工作正常,但我没有找到一种方法来更改文本字段中的图标的颜色,特别是放大镜和x按钮.

我发现这个Objective-C代码应该做我想要的但是我很难将它翻译成Swift:

(编辑:跳到工作Swift 3解决方案的第一个答案.)

UITextField *searchBarTextField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];

// Magnifying glass icon.
UIImageView *leftImageView = (UIImageView *)searchBarTextField.leftView;
leftImageView.image = [LeftImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
leftImageView.tintColor = [UIColor whiteColor];

// Clear button
UIButton *clearButton = [searchBarTextField valueForKey:@"_clearButton"];
[clearButton setImage:[clearButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
clearButton.tintColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)

我试图翻译成Swift:

let textField = searchController.searchBar.valueForKey("searchField") as! UITextField
// These two work fine.
textField.backgroundColor = UIColor.blackColor()
textField.textColor = UIColor.blackColor()

var glassIcon = textField.leftView
// This would work.
//glassIcon.hidden = true

// This does not have any effect.
//glassIcon?.tintColor …
Run Code Online (Sandbox Code Playgroud)

uitextfield uiimage uisearchbar swift

4
推荐指数
1
解决办法
3515
查看次数

在当前活动的 UIViewController 中呈现 UIAlertController

我有一个计时器,在完成时显示警报。此警报视图应显示在用户当前所在的视图控制器中。

我的感觉是这可以比以下更有效:

我现在这样做的方法是向我的 5 个视图控制器中的每一个提供通知的观察者,以及创建和呈现该警报的方法。

有没有办法只设置一次警报,然后将其显示在当前处于活动状态的视图控制器中?

这是我的代码:

// I've got the following in each of my view controllers.

// In viewDidLoad()
override func viewDidLoad() {
  super.viewDidLoad()
  NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SonglistViewController.presentSleepTimerFinishedAlert(_:)), name: "presentSleepTimerFinishedAlert", object: nil)
}


func presentTimerFinishedAlert(notification: NSNotification) {
  let alertController = UIAlertController(title: "Timer finished", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
  alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
  presentViewController(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

非常感谢您的任何想法!

alert uiviewcontroller ios swift

4
推荐指数
1
解决办法
6383
查看次数

添加到UITableViewCell的图像在滚动时显示在错误的行中

我在选择它时将图像添加到表视图行(实际上,我似乎将其添加到行的单元格中)(并在再次选择时删除).表视图由原型单元组成.

这是有效的,但当我滚动并回到我之前选择的行时,图像将在另一行.此外,图像也出现在其他行中.

我猜这是因为滚动时重复使用单元格.这是一个小示例项目的代码:

import UIKit

class MyTableViewController: UITableViewController {

  // Using integers for simplicity, should work with strings, too.
  var numbers = [Int]()

  override func viewDidLoad() {
    super.viewDidLoad()

    for i in 0..<50 {
      numbers.append(i)
    }
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("TestCell", forIndexPath: indexPath)
    cell.textLabel?.text = "\(numbers[indexPath.row] + 1)"

    return cell
  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numbers.count
  }

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) …
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift

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

App Store 上显示的应用本地化版本信息

在 中App Store Connect,App Store 上本地化替代项中文本字段“ What's new in this version”和“ ”显示的默认版本信息是哪个?Promotional Text

以下假设正确吗?

版本信息以应用程序的主要语言“美国英语”中的英语和西班牙语“西班牙语(西班牙)”提供:

  1. 西班牙语(西班牙)以及所有其他西班牙语变体将使用手动提供的西班牙语文本。
  2. 所有其他使用英语或其他语言的应用商店将显示手动提供的英文文本。

或者两个文本字段都留空实际上不会在 App Store 中显示任何内容?

我觉得不必为所有本地化版本手动复制和粘贴英文版本(仅使用本地化关键字)。

如果您已经对此进行了测试或看到了实际行为,我将非常感谢您的回答。

localization ios app-store-connect

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

提交至 Apple App Store:指定品牌名称为个人开发者?

我确实在 Apple 注册为个人开发者。

但是,我不希望在 App Store 中的应用程序名称下方显示我的真实姓名,而是一个“品牌”名称。

提交App到App Store时是否可以指定这样的名称?如果没有,除了重新注册为公司之外还有其他方法吗(我没有公司,也不想提交所有可能的文件)。

我读过人们在提交他们的应用程序后提出了类似的问题,我认为我可以预见性地避免麻烦。

非常感谢

app-store itunes-store ios app-store-connect

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