标签: init

Python,__ init__和自我混淆

好吧,所以当我遇到这个时,我正在看一些来源:

>>> def __parse(self, filename):
...         "parse ID3v1.0 tags from MP3 file"
...         self.clear()
...         try:
...             fsock = open(filename, "rb", 0)
...             try:
...                 fsock.seek(-128, 2)
...                 tagdata = fsock.read(128)
...             finally:
...                 fsock.close()
...             if tagdata[:3] == 'TAG':
...                 for tag, (start, end, parseFunc) in self.tagDataMap.items():
...                     self[tag] = parseFunc(tagdata[start:end])
...         except IOError:
...             pass
... 
Run Code Online (Sandbox Code Playgroud)

所以,我决定测试一下.

 >>> __parse("blah.mp3")
Run Code Online (Sandbox Code Playgroud)

而且,我收到了这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __parse() takes exactly 2 arguments (1 …
Run Code Online (Sandbox Code Playgroud)

python init self

3
推荐指数
1
解决办法
3416
查看次数

如何在所有控制器init()函数中运行相同的行?

我在所有控制器中都需要相同的2行,每个控制器都有自己的初始化逻辑,但这两行对所有控制器都是通用的.

public function init()
{
    $fm =$this->_helper->getHelper('FlashMessenger');
    $this->view->messages = $fm->getMessages();
}
Run Code Online (Sandbox Code Playgroud)

如何避免重复代码?

更新:

好吧,FlashMessenger只是一个例子,假设我需要在每个动作中写一个日志行,除了'someAction'@'someController'.所以新的共同线应该是.

$this->logger = new Zend_Log();
$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/../logs/log.txt');
$this->logger->addWriter($writer);
$this->logger->log('Some Message',Zend_Log::DEBUG);
Run Code Online (Sandbox Code Playgroud)

问题是,我应该在哪里放置这些行,以避免在每个控制器的所有init()中重复它们.这些行应该放在bootstrap?如果是这样:如何跳过'someAction'的日志行.或者我应该实现'BaseController'并使我的所有控制器从它扩展.如果是这样:我如何自动加载它?(致命错误:未找到类'BaseController').

zend-framework controller init zend-framework-mvc

3
推荐指数
1
解决办法
2977
查看次数

是什么 - [NSURL _fastCharacterContents] :?

所以我在一个方法中调用它:

-(id)initWithContentURL:(NSString *)url {
if (self = [super init]) {
    NSLog(@"xSheetMusicViewController - %@",url);
    // Casting an NSString object pointer to a CFStringRef:
    CFStringRef cfString = (CFStringRef)url;        
    CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), cfString, NULL, NULL);
    pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

哪一个在NSLog的标记处发生了崩溃:

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), cfString, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

我从未见过这个奇妙的小错误.这是崩溃日志:

SheetMuse[83550:b603] -[NSURL _fastCharacterContents]: unrecognized selector sent to instance 0x4ec35f0
2011-09-22 17:36:22.921 SheetMuse[83550:b603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL _fastCharacterContents]: unrecognized selector sent to instance 0x4ec35f0'
*** Call …
Run Code Online (Sandbox Code Playgroud)

init nsstring cfstring ipad ios

3
推荐指数
1
解决办法
3768
查看次数

检查对象是否为零时出了什么问题

我已经阅读了苹果的文档http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html

它说我们可以检查对象是否为空...

 XYZPerson *somePerson;
    // somePerson is automatically set to nil
 if (somePerson != nil) {
        // somePerson points to an object
    }
Run Code Online (Sandbox Code Playgroud)

很可能我做错了什么,但需要你的帮助来找到它是什么.我有一个C2类,我创建C2对象但没有初始化,当检查下面的代码时,app写入NOTNİL输出.我做错了什么.

谢谢你的帮助.

C2 * o3;

    if (o3 != nil) {
        NSLog(@"NOT N?LLLL");
    }else{
        NSLog(@"NOT");
    }
Run Code Online (Sandbox Code Playgroud)

null object objective-c init

3
推荐指数
1
解决办法
449
查看次数

如何在BusyBox关闭时调用停止脚本?

我在/ etc/inittab中运行带有条目的BusyBox

::sysinit:/etc/init.d/rcS
Run Code Online (Sandbox Code Playgroud)

rcS脚本在启动时调用/etc/rc.d/中的所有启动脚本.

如何通过调用/etc/rc.d/xxx停止调用BusyBox小程序"poweroff","halt"或"rebo​​ot"来告诉BusyBox init关闭所有服务?

shutdown init busybox system-shutdown

3
推荐指数
1
解决办法
1699
查看次数

init中的yii2重定向是否有效?

class TestController extends Controller {
    function init() {
       if(!(strtolower(yii::$app->requestedRoute) == "account/login" || empty(yii::$app->requestedRoute)))
       {
           if (false===$this->isLogin()) {

               return $this->redirect('/login');   //it works, will redirect

           }
       } else {
           if($this->isLogin()) {
               return $this->redirect('/purchaser/manifest');   //not work, won't redirect
                //echo $this->redirect('/purchaser/manifest');    //work


           }
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

我有覆盖控制器.当我尝试过滤时,我发现了这个问题.我很困惑,有什么帮助吗?

redirect init yii2

3
推荐指数
1
解决办法
2144
查看次数


如何在Storyboard中使用ViewController的自定义init

我有一个故事板,其中放置了所有的viewControllers.我用的StoryboardID是:

AddNewPatientViewController * viewController =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"addNewPatientVC"];
 [self presentViewController:viewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

AddNewPatientViewController我添加了一个自定义的init方法或构造函数,你可以说:

-(id) initWithoutAppointment
{
    self = [super init];
    if (self) {
        self.roomBedNumberField.hidden = true;
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是通过使用上面提到的视图控制器的视图,我怎么能用我init做的这个定制init.

我已尝试将此作为上述代码的替换,但它不起作用.

AddNewPatientViewController *viewController = [[AddNewPatientViewController alloc] initWithoutAppointment];
 [self presentViewController:viewController animated:YES completion:nil]; 
Run Code Online (Sandbox Code Playgroud)

constructor objective-c init ios uistoryboard

3
推荐指数
2
解决办法
3413
查看次数

如何将(Swift4)init添加到Decodable协议

我正在尝试创建一个Codable扩展,它能够仅使用json字符串初始化Decodable(Swift 4)对象.那应该是什么工作:

struct MyObject: Decodable {
   var title: String?
}

let myObject = MyObject(json: "{\"title\":\"The title\"}")
Run Code Online (Sandbox Code Playgroud)

我认为这意味着我应该创建一个使用Decoder调用self.init的init.这是我提出的代码:

public init?(json: String) throws {
    guard let decoder: Decoder = GetDecoder.decode(json: json)?.decoder else { return }
    try self.init(from: decoder) // Who what should we do to get it so that we can call this?
}
Run Code Online (Sandbox Code Playgroud)

该代码能够获取解码器,但是在调用init时出现编译器错误.我得到的错误是:

在self.init电话之前使用'self'

这是否意味着无法在Decodable协议中添加init?

有关完整的源代码,请参阅github上的可编码扩展

更新: 从@appzYourLive调试下面的解决方案后,我发现我与init(json:Decodable和Array上的初始化程序有冲突.我刚刚向GitHub发布了新版本的扩展.我还添加了解决方案作为这个问题的新答案.

json init swift4 codable decodable

3
推荐指数
1
解决办法
3054
查看次数

Kotlin阵列初始化中的初始化调用顺序

在数组的构造函数中,是否可以保证索引的递增顺序将调用init函数?

这是有道理的,但我在文档中找不到任何此类信息:https : //kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/-init-.html#kotlin.Array%24%28kotlin .Int%2C + kotlin.Function1%28%28kotlin.Int%2C + kotlin.Array.T%29%29%29%2Finit

arrays init kotlin

3
推荐指数
1
解决办法
133
查看次数