Tag: swift2

string字符数与iOS7兼容

使用Swift 2,用于支持iOS 7及更高版本的项目。 我试图通过使用string.characters.count来查找string中的字符数。 但是在快速帮助检查器中 ,它显示了iOS(9.0和更高版本)的可用性。 我已经在安装了iOS 8.4的iPad上使用了这种方法,它没有给出任何错误,并给了我正确的string长度。 如果我在装有iOS 7的设备上使用它,会发生什么?

在swift中准备数组中的元素

我有两个viewcontrller vc1和vc2。 在vc1中是用jsonparsing的tableview,它显示了一些名字。 vc1: var names: [String] = [] // Displays name (Ana, Mike, John ….) override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TVC cell.lable.text = name[indexPath.row] override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let someName: vc2 = segue.destinationViewController as! vc2 someName.nameFromVC1 = //need code […]

Swift 2.0exception处理

我们想知道整个SWIFT代码块的error handling机制。 因为,在迅速,有一些技术用于error handling,其中一些就像使用guard或if let 。 我们也可以使用do – catch语句来实现它。 但是我们在使用单个try-catch块来处理大量代码时遇到了困难,就像我们在Objective-C中所做的那样。 在Objective-C这个块很容易处理任何代码行中的错误。 所以我们想迅速地知道这种机制。 现在,我们知道如果我们需要处理错误,那么对每行使用guard , if-let语句,或者编写一些自定义的方法来抛出错误,并在do-catch块中使用该方法。 那么Swift中有没有什么机制可以像Objective-C一样与try-catch块并行,所以我们不需要为整行代码中的每一行代码编写if-let或guard语句。 这就是我们如何使用if-let和guard让单行声明为null处理: if-let实现示例代码片段: – if let title = dict["title"].string { let myTitle = title } 但是下面提到了我们希望在代理函数中用于try-catch块的swift解决scheme。 Objective-C使用try-catch块的示例代码片段 – (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { @try { static NSString *itemIdentifier = @"CustomCell"; //Some line of code where exception may occur… //Some […]

Swift 2 – 调用`didDeselectItemAtIndexPath`时发生致命错误

我有一个UICollectionView我使用函数didSelectItemAtIndexPathselect一个单元格,并更改其alpha。 在UICollectionView有12个单元格。 为了将取消选中的单元格返回到alpha = 1.0我使用函数didDeselectItemAtIndexPath 。 到目前为止,代码工作,但是,当我select一个单元格,并滚动UICollectionView应用程序崩溃在线let colorCell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)! 里面的错误取消selectfunction: 致命错误:意外地发现零,而解包一个可选的值(lldb) 我想我需要重新加载收集视图,但如何重新加载并保持单元格select? override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let colorCell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)! colorCell.alpha = 0.4 } override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) { let colorCell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)! colorCell.alpha = 1.0 }

试图创build一个本地通知,但得到错误“不能调用非函数typesuilocalnotification值”在swift 2

import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var dateComp:NSDateComponents = NSDateComponents() dateComp.year = 2015; dateComp.month = 11; dateComp.day = 20; dateComp.hour = 0; dateComp.minute = 10; dateComp.timeZone = NSTimeZone.systemTimeZone() var calendar:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! var date:NSDate = calendar.dateFromComponents(dateComp)! //var date:NSDate = calendar.dateFromComponents(dateComp) var notification:UILocalNotification = UILocalNotification() notification.category = "FIRST_CATEGORY" notification.alertBody = […]

如何创build转换到SKAction?

我创build的function,每10秒更改我的背景颜色,我想添加转换时,它改变了背景的颜色。 游戏场景: let wait = SKAction.waitForDuration(10) let block = SKAction.runBlock({ [unowned self] in self.backgroundColor = UIColor.randomColor() }) let sequence = SKAction.sequence([wait,block]) runAction(SKAction.repeatActionForever(sequence), withKey: "colorizing") 感谢帮助!

预取后从高速caching中采集图像

我正在使用Kingfisher框架来预取图像。 到Kingfisher框架的链接是: https : //github.com/onevcat/Kingfisher 这是我写的代码: func downloadImages() { if(self.albumImagePathArray.isEmpty == false) { //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { let urls = self.albumImagePathArray.map { NSURL(string: $0)! } let prefetcher = ImagePrefetcher(urls: urls, optionsInfo: nil, progressBlock: nil, completionHandler: { (skippedResources, failedResources, completedResources) -> () in print("These resources are prefetched: \(completedResources)") }) prefetcher.start() } } 我不确定如何使用这个框架,因为我对App开发很陌生。 一旦我预取了图像,如何从caching中获取这些预取图像。 我想从caching中select这些图像,并把它们放入一个UIImage数组中。 以前我没有从caching中select,但我每次都加载URL,并把它们称为albumImagePathArray,通过contentsOfURLstring数组我将它转换成UIImage,因此它可以被放入albumImages中,如下面的代码所示: var […]

AGPushNote替代Swift?

就像标题所说,你知道AGPushNote的替代scheme吗? 为了更好地理解当我在SO上发现了什么,我想要检测什么时候推送通知传入并且应用程序处于活动状态,这样我就可以防止通知出现,并执行一些我自己的东西。

迅速。 SIGABRT:将“__NSCFDictionary”types的值转换为“NSMutableArray”

我正在制作转换器应用程序。 我想保存历史转换。 我看到这个教程,它工作正常,但是当我试图在我的应用程序上使用它,我得到一个SIGABRT。 无法将“__NSCFDictionary”(0x57945c)types的值转换为“NSMutableArray”(0x5791c8) 我得到这个 notesArray = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableArray 编辑: AppDelegate中: import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var plistPathInDocument:String = String() func preparePlistForUse(){ let rootPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0] plistPathInDocument = rootPath.stringByAppendingString("/historic.plist") if !NSFileManager.defaultManager().fileExistsAtPath(plistPathInDocument){ let plistPathInBundle = NSBundle.mainBundle().pathForResource("historic", ofType: "plist") as String! do { […]

如何在iOS项目中正确configurationRealm DB文件(<db_name> .realm)的path?

对不起我的英语不好。 我开发了一个需要自己的本地数据存储的移动应用程序。 我select了Realm作为数据库pipe理系统。 在研究Realm文档的过程中,我对数据库devise没有任何问题,它是规范化,CRUD操作以及与代码直接相关的所有事情。 我感到困惑。 我在模拟器中testing我的应用程序。 我没有创build本地数据库。 我刚刚做了它,因为它是在Realm的文档中写的。 领域创build自己的数据库,我没有指定任何设置。 我的文件与数据库的位置如下: /Users/macbookpro/Library/Developer/CoreSimulator/Devices//EFECD945-285C-494F-8C1B-950D9AA05147/data/Containers/Data/Application/8D71FF0A-60D2-4875-96BB-36955E80D505/Documents/default.realm。 我很不方便地应用这个path来分析数据库中的logging。 该文件存储在这个path是无法访问的Realm浏览器,因为我需要root权限。 我已经看到人们将他们的.realm文件存储在项目目录中。 请回答我 – 我该怎么办? 这是否是正确的select? 我已经多次看到数据库文件path出现的代码。 例如,在这里: https : //github.com/pietbrauer/CarthageRealmUploadFailureExample/blob/master/Carthage.checkout/realm-cocoa/examples/ios/swift/Migration/AppDelegate.swift我不知道如果这是正确的方向。 因此,我想: 1)手边有一个数据库文件,并能够快速引用它; 2)知道algorithm – 如何设置自己的path到Realm数据库文件; 3)了解真实设备上的域configuration,以确保安全性和数据完整性。 非常感谢大家!