以下是否有区别:
懒变量:
lazy var profileImageIsLoaded : Bool = {
return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0)
}()
Run Code Online (Sandbox Code Playgroud)
功能:
func profileImageIsLoaded() -> Bool {
return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0)
}
Run Code Online (Sandbox Code Playgroud)
计算物业:
var profileImageIsLoaded : Bool {
return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0)
}
Run Code Online (Sandbox Code Playgroud)
什么方法最好用?
我会多次调用函数/变量,所以我的问题也是懒惰变量是否"更新"或者它们是否只获得一次值.
正如 Mozilla 的 JavaScript 参考中所述: https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_static_methods
这就是私有静态方法应该如何工作:
class Foo {
static #privateStaticMethod() {
return 42;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在 NodeJS v12.13.0 中使用它时,会抛出以下语法错误:
static #privateStaticMethod() {
^
SyntaxError: Unexpected token '('
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (.../foo.js:8:14)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
Run Code Online (Sandbox Code Playgroud)
查看浏览器兼容性页面,从 v12 开始应该支持私有静态方法。
这是为什么?
我有2个类,第一个类名为"store",我在其中创建一个调用方法的按钮:"storeSelected"位于第二个类中,名称为ExploreViewController
.
该方法应该打印"完成"并带我到另一个视图控制器.如果没有segue,则会打印"done",但是当放置segue代码时,app会崩溃.
错误是:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<Glam.ExploreViewController: 0x14ed3be20>) has no segue with identifier 'ok''
.........
// ExploreViewController Class
let _sharedMonitor: ExploreViewController = { ExploreViewController() }()
class ExploreViewController: UIViewController, UIScrollViewDelegate {
class func sharedMonitor() -> ExploreViewController {
return _sharedMonitor
}
func storeSelected(sender: UIButton) {
println("done") // it entered here and "done" is printed
self.performSegueWithIdentifier("ok", sender: self) //here is the problem
}
}
// another class named "Store"
// button is created …
Run Code Online (Sandbox Code Playgroud) 我刚刚写了一些基本的PHP代码,如下所示:
$pdo = new PDO("mysql:host=localhost;dbname=locationtracker", "xxxx", "xxxx");
$statement = $pdo->prepare("SELECT * FROM waypoints");
$result = $statement->execute();
if ($result){
echo "Success";
$resultArray = array();
$tmpArray = array();
while($row = $statement->fetch()){
print_r($row);
echo "<br>";
$tmpArray = $row;
array_push($resultArray, $tmpArray);
}
print_r(json_encode($resultArray));
}else{
die("Error.<br>");
}
Run Code Online (Sandbox Code Playgroud)
sql表'waypoints'看起来像这样:
waypoints
| x: double
| y: double
| name: varchar(255)
| city: varchar(255)
| id: Int
Run Code Online (Sandbox Code Playgroud)
所以我想将数组转换为JSON格式.听起来很简单,但我的PHP代码产生了这样的:
Success
Array ( [x] => 7.0000 [0] => 7.0000 [y] => 32.0000 [1] => 32.0000 [name] => Georgia [2] => …
Run Code Online (Sandbox Code Playgroud) 我在VC中实现了自己的inputAccessoryView,该VC由推送UINavigationController
。
VC的外观如下:注意底部的inputAccessoryView(带有按钮和文本字段的白条)
当我从屏幕的左向右滑动(以关闭当前的VC并返回)时,inputAccessoryView向下移动并消失。另外,如果我在任意位置停止滑动手势并让当前的VC跳回(这样就不会被关闭),inputAccessoryView也会向下移动并消失。
我在移动时附加了另一张照片:
VC跳回后的另一个:
如您所见,inputAccessoryView已消失。
我的一段代码:
private final lazy var inputContainerView: UIView = {
let containerView = UIView()
containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
[...]
return containerView
}()
[...]
override var inputAccessoryView: UIView? {
return inputContainerView
}
override var canBecomeFirstResponder: Bool {
return true
}
Run Code Online (Sandbox Code Playgroud) 我在Xcode 8(Swift 3)中创建NSOutlineView时遇到了麻烦.我有一个plist文件,其中包含一些我想在OutlineView中显示的信息.plist文件如下所示(示例):
Root Dictionary *(1 item)
Harry Watson Dictionary *(5 items)*
name String Harry Watson
age Int 99
birthplace String Westminster
birthdate Date 01/01/1000
hobbies Array *(2 items)*
item 0 String Tennis
item 1 String Piano
Run Code Online (Sandbox Code Playgroud)
OutlineView看起来应该非常相似,如下所示:
name Harry Watson
age 99
birthplace Westminster
birthdate 01/01/1000
> hobbies ... (<- this should be expandable)
Run Code Online (Sandbox Code Playgroud)
我已经在Google上搜索了NSOutlineView教程,但我发现的一切都是raywenderlich.com,所以我读了一下,但在我看来并不那么容易.所以我想知道你是否可以帮助我完成上面的确切示例并给我一些代码示例,特别是关于这个函数:
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {}
Run Code Online (Sandbox Code Playgroud)
我不知道该写些什么.
如果您有任何疑问,请告诉我.
在此先感谢您的亲切问候
标题很长,但我认为这最能概括我的问题。
给定一个整数n,我想用最多包含 n 个对象。
问题是数组中的对象不能为nil,但是,对象类型的构造函数可能会失败(即返回 nil
)。
我想出了两种方法来解决这个问题,我认为还有更多 - 是否有比以下两种常见的、良好的实践、众所周知或更好的解决方案?
struct MyClass {
init?() {
// ...
if (/* cond */) { return nil }
}
}
// ...
// 1. solution
struct A {
let arr: [MyClass]
let n = 6
init?() {
let arr = (0..<n).map { _ in MyClass() }
guard arr.allSatisfy({ $0 != nil }) else { return nil }
self.arr = arr as! [MyClass] // At this point we can safely …
Run Code Online (Sandbox Code Playgroud)