Tag: swift2

Swift 2 base64编码产生不同的结果

Swift 1.2 let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD); let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!; var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(nil); Swift 2 let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD); let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!; var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions()); 这两种方法产生不同的结果。 在Swift 2中,我们不能再通过nil ; apiLoginData.base64EncodedStringWithOptions(nil); 我也试过在Swift 2中传入NSDataBase64EncodingOptions.Encoding64CharacterLineLength 。但结果仍然有差异 有什么想法吗? 更新2:这不是与Swift 1.2或Swift 2的问题。奇怪的是,这些2打印出不同的结果..什么F! 更新3:结果如马丁所指出的,我在我的API_USERNAMEvariables中有一些不可见的字符 class RequestHelper: NSObject { static var […]

NSFileManager.defaultManager()。createFileAtPath()返回false

像千print()声明后,我终于明确了这个问题! 但是,我不知道如何解决这个问题。 问题在于: NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil) 根据苹果开发者指南, returns true if the operation was successful or if the item already exists, otherwise false.这行代码returns true if the operation was successful or if the item already exists, otherwise false. 这行是返回一个false ,我不完全确定为什么,因为前面的代码似乎是好的。 任何人有任何build议如何解决这个错误? 其余的代码在这里: // // ViewController.swift // Downloading An Image From The Web // // Created […]

如何在目标c中使用swift文件

我想在Objective-C代码中使用我的Swift文件。 我发现不同的链接,说如何做到这一点。 我的项目名称是:Test-Project。 我将#import“Test-Project-Swift.h”导入到我的.m文件中,以使用所需的目标c源代码。 我看了这个video ,没有问题,但是我没有find'Test-Project-Swift.h'文件。

Swift:只在返回types中有所不同的方法重载

我不断看到Swift类,其中定义了两种方法,只是返回types不同。 我不习惯在允许这样的语言(Java,C#等)中工作,所以我去寻找描述在Swift中如何工作的文档。 我什么都找不到。 我本来期待Swift书中的整个章节。 这在哪里logging? 这是我正在谈论的一个例子(我正在使用Swift 2,FWIW): class MyClass { subscript(key: Int) -> Int { return 1 } subscript(key: Int) -> String { return "hi" } func getSomething() -> Int { return 2 } func getSomething() -> String { return "hey" } } testing: let obj = MyClass() //let x = obj[99] // Doesn't compile: […]

使用未声明的types“对象”

这太奇怪了。 通常我可以理解一个没有被声明的类,但这是声明Object类本身没有被声明的。 NSObject的作品,但我的项目设置的方式我需要它是一个纯粹的Swift对象。 我的课程标题如下所示: import UIKit import Foundation class Person: Object { 我知道基础并不是真的有必要,我只是添加它,因为我不确定是否会导致我的问题。 这个问题发生在实际的项目和游乐场,以及在Xcode 6和最新的Xcode 7testing版与Swift 2.0

Realm = RLMRealm'没有成员'setDefaultRealmPath'

我已经将Realm.framework和RealSwift.framework添加到一个项目中。 和“import领土”虽然我得到这个错误: RLMRealm'没有成员'setDefaultRealmPath' let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.it.fancypixel.Done")! let realmPath = (directory.path! as NSString).stringByAppendingPathComponent("db.realm") RLMRealm.setDefaultRealmPath(realmPath) 任何想法,我似乎无法看到这个解决scheme,因为这是如此新。 提前致谢。

服务定位器模式在Swift中

我对Swift中灵活的通用Service Locatordevise模式实现感兴趣。 天真的做法可能如下: // Services declaration protocol S1 { func f1() -> String } protocol S2 { func f2() -> String } // Service Locator declaration // Type-safe and completely rigid. protocol ServiceLocator { var s1: S1? { get } var s2: S2? { get } } final class NaiveServiceLocator: ServiceLocator { var s1: S1? […]

申请后/ x-www-form-urlencoded Alamofire

我想使用Alamofire从Web API中获取不记名令牌,但我是ios和alamofire的新手。 我怎么能用Alamofire完成这个? func executeURLEncodedRequest(url: URL, model: [String : String]?, handler: RequestHandlerProtocol) { addAuthorizationHeader() Alamofire.request(.POST,createUrl(url), parameters: model, headers: headers,encoding:.Json) }

在从初始化程序返回之前,不会调用Super.init

我尝试给我的UITableViewCell类一个自定义的initilizer,但我无法弄清楚我做错了什么。 这是我的代码: init(dataObject: [NSManagedObject]!, objectAttributeValues: [String]!,placeholder: String!, segmentedControl: UISegmentedControl?, cellHeight: CGRect, cellWidth: CGRect) { self.dataObject = dataObject self.Placeholder.text = placeholder self.objectAttributeValues = objectAttributeValues if segmentedControl != nil { self.segmentedControl = segmentedControl! didHaveSegmentedControl = true } } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } 我试图调用super.init(frame:CGRect(…)),但通过实现这个我得到了另一个错误: 必须调用超类'UITableViewCell'的指定初始值设定项 我能做什么? 万分感谢!

Swift 2在调用中额外的参数“错误”

您好我正在更新我的项目从Swift Swift2与Xcode 7,我得到这个CoreData错误: Extra argument 'error' in call 在这一行 if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil { 编辑 这是我的代码: lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This […]