小编Cos*_*ows的帖子

使用适用于iOS 10的UNUserNotificationCenter

尝试使用Firebase注册远程通知,但是在实现以下代码时,我收到错误:

UNUserNotificationCenter仅适用于iOS 10.0或更高版本

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        var soundID: SystemSoundID = 0
        let soundFile: String = NSBundle.mainBundle().pathForResource("symphony", ofType: "wav")!
        let soundURL: NSURL = NSURL(fileURLWithPath: soundFile)
        AudioServicesCreateSystemSoundID(soundURL, &soundID)
        AudioServicesPlayAlertSound(soundID)
        Fabric.with([Twitter.self])


        //Firebase configuration
        FIRApp.configure()

        //Resource code from stackoverflow to create UNUserNotificationCenter
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()
        return true
    }
Run Code Online (Sandbox Code Playgroud)

通过简单的"修复它"不能通过基于操作系统版本号创建if语句来解决我的问题.对于UserNotifications框架,我应该做什么或想到这个解决方案?

apple-push-notifications swift firebase-cloud-messaging

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

在 Swift 中,协议扩展是否允许函数体?

我正在阅读一个教程,我注意到作者扩展了他们的称为 Activity 的协议,并在他们的代码中编写了函数的主体。这确实可以编译,但我的印象是协议仅显示方法签名,或者如果它确实实现了主体,那么它将是一个变异函数。下面的代码没有对其函数之一使用变异,但它仍然可以运行并且可以工作!有人可以解释这种现象或确认协议扩展可以有方法体吗?

import CareKit
import SwiftyJSON

enum ActivityType: String {
        case Intervention
        case Assessment
}

enum ScheduleType: String {
    case Weekly
    case Daily

}

enum StepFormat : String {
    case Scale
    case Quantity
}

protocol Activity {

    var identifier : String  { get set}
    var groupIdentifier : String  { get set}
    var title : String  { get set}
    var colour : UIColor?  { get set}
    var text : String  { get set}
    var startDate : Date  { get set} …
Run Code Online (Sandbox Code Playgroud)

swift swift-extensions swift-protocols

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

如果没有swift 3中的更多上下文,为什么这个元组含糊不清?

在查看几个月前运行的代码之后,可以解释一下如何修复此错误吗?显示的错误显示"表达式的类型不含更多上下文".

var products = [("Kayak","A boat for one person","Watersports",275.0,10),
                ("Lifejacket","Protective and fashionable","Watersports",48.95,14),
                ("Soccer Ball","FIFA-approved size and weight","Soccer",19.5,32),
                ("Corner Flags","Give your playing field a professional touch","Soccer",34.95,1),
                ("Stadium","Flat-packed 35,000-seat stadium","Soccer",79500.0,4),
                ("Improve your brain efficiency by 75%","Chess",16.0,8),
                ("Unsteady Chair","Secretly give your opponent a disadvantage","Chess",29.95,3),
                ("Human Chess Board","A fun game for the family","Chess",75.0,2),
                ("Bling-Bling King","Gold-plated,diamon-studded King","Chess",1200.0,4)]
Run Code Online (Sandbox Code Playgroud)

xcode tuples ios swift

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

使用尖括号的 Swift 数组语法

在博客上阅读有关 Big O Notation 的内容时,引用了以下代码行:

let numberList : Array<Int> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
Run Code Online (Sandbox Code Playgroud)

它让你措手不及,因为它在尖括号内有关键字类型 Int,我认为这就是泛型语法的工作原理。因此,我在操场上调查了这行代码是否与我习惯看到的相同:

let numberList : Array<Int> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
let numberList2 : [Int] = [1,2,3,4,5,6,7,8,9,10]
if numberList == numberList2{
    print("They're the same")
}
Run Code Online (Sandbox Code Playgroud)

在继续之前,我只想确定并询问其他人这是否绝对相同,并且您选择哪种格式并不重要。

另外,我认为这不是重复的,因为在发布此问题之前提出问题时,我已经单击了所有快速数组链接。

arrays syntax ios swift

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

在Apple的NSObject文档中,"接收器"的概念是什么?

我正在研究Swift中的面向对象编程,我认为一个很好的起点是NSObject,因为所有对象都继承自这个基类.在Apple的文档中NSObject,有些区域指的是"接收器"."接收者"是指一个实例NSObject吗?

terminology objective-c nsobject swift

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

了解为什么在函数内部实例化后使用弱释放存储属性的原因

即使在阅读了Swift关于ARC的文档之后,我仍然无法理解为什么属性设置为nil,即使它在函数中被实例化也是如此.我有以下示例代码:

import UIKit

class ItemListViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView = UITableView()

    }


}
Run Code Online (Sandbox Code Playgroud)

以下是测试运行的示例代码:

import XCTest
@testable import PassionProject

class ItemListViewControllerTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class. …
Run Code Online (Sandbox Code Playgroud)

ios automatic-ref-counting xctest swift

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

为什么XCTestCase会覆盖XCTest的设置方法?

我想以为我了解继承的概念,但是显然我不明白,因为如果XCTest在其类中提供设置方法,为什么在XCTestCase中存在设置方法,我感到困惑。XCTestCase是XCTest的子类,但是在阅读了Apple文档之后,两者看起来没有什么不同。

import XCTest
@testable import FirstDemo

class FirstDemoTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to …
Run Code Online (Sandbox Code Playgroud)

ios xctest swift xctestcase

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

在iOS中,将视图添加为子视图与将视图分配给视图属性之间是否存在差异?

我试图理解iOS编程中的任何歧义,所以我看过开发人员将其主视图分配给viewController的view属性的教程,如下所示:

let sceneView = ARSCNView(frame: self.view.frame)
view = sceneView
Run Code Online (Sandbox Code Playgroud)

但是我也看到开发人员将他们的主视图作为子视图添加到view属性,如下所示:

let sceneView = ARSCNView(frame: self.view.frame)
view.addSubView(sceneView)
Run Code Online (Sandbox Code Playgroud)

这两个过程是相同的还是不同的?

uiviewcontroller uiview addsubview ios

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

"联合国"在UNNotification中代表什么?

多年来我注意到Apple在使用Swift时已经删除了诸如"NS"之类的前缀.现在,当我浏览本地通知教程时,我看到UIUserNotificationSettings已被弃用,它要求我们使用UNNotificationSettings.我明白'NS'代表下一步所以有可能是'UN'代表什么?

cocoa-touch ios swift

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

为 AVURLAssets 设置 HTTP 标头字段

我正在尝试加载一个需要标头中的令牌的视频文件。我在 Stackverflow 上了解到,通过查看以下问题,这可能是可能的。问题是我不相信 AVURLAssetHTTPHeaderFieldsKey 是公开的,或者我错误地编写了资产变量。我想做的是将令牌添加到标头中,以便 AVPlayer 加载视频文件。到目前为止,这是我的代码,它只显示播放器,但不加载视频,我猜测是因为我的标题设置不正确:

NSURL *videoURL = [NSURL URLWithString:cell.media[@"redirectionUrl"]];

        NSMutableDictionary * headers = [NSMutableDictionary dictionary];
        [headers setObject:[CMUser currentUser].token forKey:@"Authorization"];

        AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];


        AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];

        AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];

        AVPlayerViewController *playerViewController = [AVPlayerViewController new];

        playerViewController.player = player;

        playerViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;

        [self presentViewController:playerViewController animated:YES completion:nil];  
Run Code Online (Sandbox Code Playgroud)

objective-c http-headers ios avplayer avasset

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