我有一个具有常量ivar的Swift类(它们现在被称为实例常量吗?).要将值设置为此常量,我需要调用所需对象的初始化程序并传递自身.但是,我不被允许,因为我需要首先初始化所有值,然后调用super.init(),之后我被允许访问self.那么在这种情况下该怎么办?
class Broadcaster: NSObject, CBPeripheralManagerDelegate {
let broadcastID: NSUUID
let bluetoothManager: CBPeripheralManager
init(broadcastID: NSUUID) {
self.broadcastID = broadcastID
let options: Dictionary<NSString, AnyObject> = [ CBPeripheralManagerOptionShowPowerAlertKey: true ]
self.bluetoothManager = CBPeripheralManager(delegate: self, queue: nil, options: options) // error: 'self' used before super.init call
super.init()
}
}
Run Code Online (Sandbox Code Playgroud) 关于在模块或库中使用"SELF"的快速问题.基本上什么是"SELF"的范围/上下文,因为它与模块或库有关,如何正确使用?有关我正在讨论的示例,请查看安装了"restful_authentication"的"AuthenticatedSystem"模块.
注意:我知道'self'在其他语言中等同于'this'以及'self'如何在类/对象上操作,但是在模块/库的上下文中没有"自我".那么,在没有类的模块中,自我的上下文是什么?
if (view.annotation.title as String!) == "Helgoland " {
currentLong = 7.889021
currentLat = 54.180210
url = "google.com"
let alertController: UIAlertController = UIAlertController(title: "Change Map Type", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Back", style: UIAlertActionStyle.Cancel, handler: nil)
let button1action: UIAlertAction = UIAlertAction(title: "Product Page", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
performSegueWithIdentifier("showShop", sender: self)
})
let button2action: UIAlertAction = UIAlertAction(title: "Video", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
youtubeVideoID = "XX"
UIApplication.sharedApplication().openURL(NSURL(string: "http://www.youtube.com/watch?v=\(youtubeVideoID)"))
}) …Run Code Online (Sandbox Code Playgroud) 我可以编写一个返回iteself的函数吗?
我正在阅读关于闭包的一些描述 - 参见例6 - 函数返回一个函数,所以你可以调用func()();有效的JavaScript.
所以我想知道一个函数可以以这样的方式返回自己,你可以无限期地将它链接到它自己:
func(arg)(other_arg)()(blah);
Run Code Online (Sandbox Code Playgroud)
使用argumentsobject,callee还是caller?
我在这里读到" 自我指的是当前的窗口或形式 ".
在这种情况下,Self似乎没有引用当前的形式:
<form><input type="text" onkeyup="alert(self.foo.value)" name="foo"></form>
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下它可以工作(参考窗口):
<form><input type="text" onkeyup="alert(self.document.forms[0].foo.value)" name="foo"></form>
Run Code Online (Sandbox Code Playgroud)
那你什么时候才能使用selfDOM属性window呢?
我在一些外国代码中读过一些东西,我想检查一下我的假设:
@synchronized(self)用于self在设置属性时删除前缀.
所以在我下面的例子中,我正在设置strText实例,而不仅仅是局部变量,对吧?
- (void)myfunction{
NSString * strText = @"var in function";
@synchronized(self)
{
strText = @"var class (self.strText)";
}
}
Run Code Online (Sandbox Code Playgroud) 我不想写长篇文章,因为这是一个简短的问题.PHPUnit测试包含几个静态的方法.例如所有那些\PHPUnit\Framework\Assert::assert*()方法以及identicalTo,equalTo.
我的IDE(具有IntelliSense /自动完成功能)不接受使用$this,但使用self.我已经知道应该通过类调用静态函数,而不是对象self.
什么更正确?
$this->assertTrue('test');
Run Code Online (Sandbox Code Playgroud)
要么
self::assertTrue('test');
Run Code Online (Sandbox Code Playgroud)
?
(如果"$ this"更正确,你能否指出为什么我们不应该使用"自我"?)
我以为我开始掌握编程的"Python方式".类的方法接受self作为第一个参数来引用正在调用该方法的上下文的类的实例.@ classmethod装饰器引用一个方法,其功能与类相关联,但不引用具体实例.
那么,如果没有实例引用调用该方法,那么@classmethod(规范'self')的第一个参数是什么意思?
我正在学习如何each在ruby中工作的细节,我尝试了以下代码行:
p [1,2,3,4,5].each { |element| el }
Run Code Online (Sandbox Code Playgroud)
结果是一个数组
[1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
但我不认为我完全理解为什么.为什么each同一个数组的返回值?难道每个都不提供迭代方法吗?或者只是该each方法返回原始值的常见做法?
我想知道当静态子类扩展静态父类时,使用self ::和parent ::之间有什么区别
class Parent {
public static function foo() {
echo 'foo';
}
}
class Child extends Parent {
public static function func() {
self::foo();
}
public static function func2() {
parent::foo();
}
}
Run Code Online (Sandbox Code Playgroud)
func()和func2()之间有什么区别吗?如果有,那么它是什么?
谢谢
问候
self ×10
javascript ×2
php ×2
ruby ×2
swift ×2
arrays ×1
class ×1
class-method ×1
dom ×1
each ×1
function ×1
initializer ×1
ios ×1
keyword ×1
objective-c ×1
oop ×1
parent ×1
phpunit ×1
python ×1
scope ×1
static ×1
synchronized ×1
unit-testing ×1