我试图根据不同的字符串长度计算UILabel的高度.
func calculateContentHeight() -> CGFloat{
var maxLabelSize: CGSize = CGSizeMake(frame.size.width - 48, CGFloat(9999))
var contentNSString = contentText as NSString
var expectedLabelSize = contentNSString.boundingRectWithSize(maxLabelSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(16.0)], context: nil)
print("\(expectedLabelSize)")
return expectedLabelSize.size.height
}
Run Code Online (Sandbox Code Playgroud)
以上是我用来确定高度但不起作用的当前功能.我非常感谢能得到的任何帮助.我会在Swift中给出答案而不是Objective C.
我开始在iOS 8或更高版本上使用Realm并查看Realm中的文档.我注意到所有属性都在dynamic
它们前面有关键字.Realm需要这个吗?我已经阅读了关于关键字的Apple文档,可以在这里找到.https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html
我想在Realm中使用一个可选的Int并且我想到了一个老错误.
码
dynamic var reps: Int? = nil
Run Code Online (Sandbox Code Playgroud)
错误
'Property cannot be marked dynamic because its type cannot be represented in Objective-C'
Run Code Online (Sandbox Code Playgroud)
我使用Realm 0.96.1和XCode 7.1
我在Realm文档中理解它表示Int
不支持Optional
但是https://twitter.com/realm/status/656621989583548416.这是来自Realm twitter,这就是为什么我感到困惑.是Optional Int
支持还是仍然没有?
我不知道如何使用Realm进行排序.我目前的代码是.
data = realm.objects(WorkoutSet)
data = data!.sorted("date")
Run Code Online (Sandbox Code Playgroud)
我要排序日期的Int
从高数量低的数字.文档需要更多信息,GitHub链接会抛出一条404
消息.
我有一个Realm Object建模如此
class WorkoutSet: Object {
// Schema 0
dynamic var exerciseName: String = ""
dynamic var reps: Int = 0
// Schema 0 + 1
dynamic var setCount: Int = 0
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试执行迁移.
在我的AppDelegate
导入内RealmSwift
.
在didFinishLaunchWithOptions
我调用的函数内
Migrations().checkSchema()
Run Code Online (Sandbox Code Playgroud)
迁移是在另一个文件中声明的类.
在该文件中有一个声明为这样的结构.
func checkSchema() {
Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1, …
Run Code Online (Sandbox Code Playgroud) 在处理UIImage和资产目录时,我正在尝试使用Enums和connivence初始化程序使代码更安全.我的代码如下.
import UIKit
extension UIImage {
enum AssetIdentifier: String {
case Search = "Search"
case Menu = "Menu"
}
convenience init(assetIdentifier: AssetIdentifier) {
self.init(named: AssetIdentifier.RawValue)
}
}
Run Code Online (Sandbox Code Playgroud)
目前我收到此错误.
'Cannot invoke 'UIImage.init' with an argument of type '(named: RawValue.Type)'
Run Code Online (Sandbox Code Playgroud) 好的,所以我想创建变量,因为用户点击后每次点击都会添加一个新变量.我目前正在使用jquery和javascript我无法做到服务器端这必须在浏览器中完成.
newCount = document.getElementById('hello').innerHTML;
$('.hello').click(function(){
//set count fast enumeration
newCount++;
var hello + newCount = '<p>Hello World</p>';
});
Run Code Online (Sandbox Code Playgroud)
所以我希望变量是hello1,hello2,hello3,hello4等.
我在Swift中看过这篇文章可选的动态属性,但是我不想把它包装成一个NSObject
.这只是关于Realm数据库我没有必要的nil
属性,但我认为这是一个很好的方式来建模我的数据库.在Realm文档中可以找到https://realm.io/docs/swift/latest/,它表示支持选项.这是我的
码
dynamic var complete: Bool? = nil
Run Code Online (Sandbox Code Playgroud)
这是我的
错误
Property cannot be marked dynamic because its type cannot be represented in Objective-C
Run Code Online (Sandbox Code Playgroud)
我知道这是与上面的帖子相同的代码和错误,但我很好奇,如果Realm文档说它支持它,他们还有另一种解决方法吗?
我正在尝试为swift构建一个anagram检查器.这是我的代码.如果您不知道anagram检查器检查两个字符串是否包含相同的字符,但顺序无关紧要.
func checkForAnagram(#firstString: String, #secondString: String) -> Bool {
var firstStringArray: [Character] = []
var secondStringArray: [Character] = []
/* if case matters delete the next four lines
and make sure your variables are not constants */
var first = firstString
var second = secondString
first = first.lowercaseString
second = second.lowercaseString
for charactersOne in first {
firstStringArray += [charactersOne]
}
for charactersTwo in second {
secondStringArray += [charactersTwo]
}
if firstStringArray.count != secondStringArray.count {
return false
} else {
for …
Run Code Online (Sandbox Code Playgroud) Swift中的Lazy
或Optional
属性有什么区别?
例如,如果某人正在构建从侧面进入的导航栏,那么我认为所有这些都应该在一个之内UIViewController
。用户可能永远不会打开菜单,但有时会打开。
var menu: NavigationBar?
lazy var menu: NavigationBar = NavigationBar.initialize()
Run Code Online (Sandbox Code Playgroud)
我认为这两个可选代码都是不错的代码,因为除非需要,否则它们不会创建视图。我知道这Optional
意味着可能会有价值nil
。我也明白Lazy
,在我需要它之前,请不要担心。
具体问题
我的问题是它们的性能模式(安全性和速度)是否表示可选件更快,更安全,反之亦然?