Tag: swift

如何在swift中着色UIImage?

我有一个arrowWhite图像。 我想将此图像着色为黑色。 func attachDropDownArrow() -> NSMutableAttributedString { let image:UIImage = UIImage(named: "arrowWhite.png")! let attachment = NSTextAttachment() attachment.image = image attachment.bounds = CGRectMake(2.25, 2, attachment.image!.size.width – 2.25, attachment.image!.size.height – 2.25) let attachmentString = NSAttributedString(attachment: attachment) let myString = NSMutableAttributedString(string: NSString(format: "%@", self.privacyOptions[selectedPickerRow]) as String) myString.appendAttributedString(attachmentString) return myString } 我想在blackColour得到这个图像。 tintColor不工作..

Swift + CoreData:不能在生成的NSManagedObject子类上自动设置可选属性

我有一个名为Record的coredata实体,并有一个属性dateUpdated。 我注意到生成的NSManagedObject子类没有可选标记(?) CoreData编辑器: 生成的子类: 预期: 更新:在我的部分是乏味的 ,因为每次我想重新生成子类,这意味着我也需要手动更新所有可选值。 在子类中有一个非可选的(不带'?')会导致在赋值之前检查evalue,如下例所示: // sample value: // serverDateFormatter = "yyyy/MM/dd" // dateString = "" // Branch is a subclass of Record (see above images) var date = self.coreData.serverDateFormatter.dateFromString(dateString) if date != nil { branch.dateUpdated = date } 但是,如果xcode可以用(?)自动设置子类中的可选值,我只需要这样做: branch.dateUpdated = self.coreData.serverUTCDateFormatter.dateFromString(dateString) 在我的情况下,我有一堆属性,我需要手动标记为可选。

在Swift中,我可以使用元组作为字典中的键?

我想知道如果我可以以某种方式使用x,y对作为我的字典的关键 let activeSquares = Dictionary <(x: Int, y: Int), SKShapeNode>() 但是我得到的错误: Cannot convert the expression's type '<<error type>>' to type '$T1' 和错误: Type '(x: Int, y: Int)?' does not conform to protocol 'Hashable' 那么,我们怎样才能让它符合?

Swift的子类化 – 如何重写Init()

我有以下类,用init方法: class user { var name:String var address:String init(nm: String, ad: String) { name = nm address = ad } } 我想这个类的子类,但我不断收到super.init()部分的错误: class registeredUser : user { var numberPriorVisits: Int // This is where things start to go wrong – as soon as I type 'init' it // wants to autocomplete it for me with […]

如果不是让 – 在Swift中

有没有办法否定“如果让”在迅速? 这对我来说很愚蠢: if let type = json.type { } else { XCTFail("There is no type in the root element") } 我不能使用XCTAssertNotNil,因为json.type是一个枚举。 enum JSONDataTypes { case Object case Array case Number case String } 非常感谢 编辑:这是一个: var type: JSONDataTypes? = nil

@财产/ @综合等价于swift

我们曾经声明property在类之间传递数据如下: .h file (interface file) @property (nonatomic) double topSpeed; .m file (implementation file) @synthesize topSpeed; 现在没有interface类,如何在.swift类之间传递数据?

在iOS8中使用UISplitViewController隐藏主视图控制器

我有一个iOS7应用程序,它基于Xcode主细节模板,我正在移植到iOS8。 UISplitViewController是一个变化很大的UISplitViewController 。 在纵向模式下时,如果用户点击详细视图控制器,主视图控制器将被解除: 我也想能够以编程方式隐藏主视图控制器,如果用户轻击一行。 在iOS 7中,主视图控制器显示为popup式窗口,可以隐藏,如下所示: [self.masterPopoverController dismissPopoverAnimated:YES]; 随着iOS 8,主人不再是一个popup窗口,所以上述技术将无法正常工作。 我试图解雇主视图控制器: self.dismissViewControllerAnimated(true, completion: nil) 或者告诉分割视图控制器显示细节视图控制器: self.splitViewController?.showDetailViewController(bookViewController!, sender: self) 但到目前为止没有任何工作。 有任何想法吗?

Swift在UITextField中添加图标/图像

我想在UITextField中添加图标/图像。 图标/图像应留给占位符。 我试过这个: var imageView = UIImageView(); var image = UIImage(named: "email.png"); imageView.image = image; emailField.leftView = imageView; 谢谢。

UIView在Swift中的背景颜色

有没有办法使用Swift设置UIView背景颜色? 我知道在Objective-C中,你会使用self.view.backgroundColor = [UIColor redColor]; ,但是这在Swift中的工作方式并不一样。 我环顾四周,因为斯威夫特只有一个星期左右,我找不到答案。 有没有人有什么build议?

如何将string转换为Swift iOS中的string?

我很快就学会了将datestring转换为NSDate到string。 获取这种格式的datestring“星期四,22十月2015 07:45:17 +0000”。 我需要以MM-dd-yyyy格式显示date。 我试过下面的代码,但是,它返回“null”。 let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy" dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle let dateObj = dateFormatter.dateFromString(dateString!) print("Dateobj: \(dateObj)") 任何人都可以请帮助哪里出错? 期待帮助。 提前致谢。