以下代码在Python 2.5和3.0中按预期工作:
a, b, c = (1, 2, 3)
print(a, b, c)
def test():
print(a)
print(b)
print(c) # (A)
#c+=1 # (B)
test()
Run Code Online (Sandbox Code Playgroud)
但是,当我取消注释行(B)时,我得到了UnboundLocalError: 'c' not assigned
一行(A).的值a
和b
被正确地打印.这让我感到困惑,原因有两个:
为什么在行(A)处抛出运行时错误,因为后面的行(B)语句?
为什么变量a
和b
打印符合预期,同时c
引发错误?
我能想到的唯一解释是,赋值创建了一个局部变量,即使在创建局部变量之前,它也优先于"全局"变量.当然,变量在存在之前"窃取"范围是没有意义的.c
c+=1
c
有人可以解释一下这种行为吗?
浏览缩小的Javascript代码时,我经常会看到以下语句:
if (!''.replace(/^/, String)) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
这是做什么的?似乎任何符合ECMA的JS解释器都将替换字符串的开头String('')
,这仍然会导致一个空字符串,其否定是true
.
在什么情况下行为会有所不同?
我正在实现一个具有最小功能的自定义UIButton..h文件:
#import <Foundation/Foundation.h>
@interface CustomButton : UIButton {
}
@end
Run Code Online (Sandbox Code Playgroud)
我在.m文件中的第(A)行遇到编译错误:
- (id)initWithCoder:(NSCoder *)coder {
if(self = [super initWithCoder:coder]) {
CALayer *layer = [self layer];
NSLog(@"layer=%@",layer);
NSLog(@"delegate=%@",[layer delegate]);
#ifdef __IPHONE_3_0
layer.cornerRadius = 4.0f; // (A) error: request for member 'cornerRadius' in something not a structure or union
#endif
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
如果我注释掉(A)行,我得到以下输出:
2009-10-08 17:35:06.681 MyApp[2596:4e07] layer=<CALayer: 0x3cdf520>
2009-10-08 17:35:06.683 MyApp[2596:4e07] delegate=<CustomButton: 0x3cdaff0; baseClass = UIButton; frame = (9 212; 255 55); opaque = NO; autoresize = RM+BM; layer = …
Run Code Online (Sandbox Code Playgroud) 我有两个PartialFunctions f
和g
.它们没有副作用,可以快速执行.将它们组成另一个部分函数的最佳方法是什么h
,
h.isDefinedAt(x)
iff f.isDefinedAt(x) && g.isDefinedAt(f(x))
?
如果h
函数返回Option
而不是部分函数,也可以.
我很失望,f andThen g
没有做我想做的事:
scala> val f = Map("a"->1, "b"->2)
f: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2)
scala> val g = Map(1->'c', 3->'d')
g: scala.collection.immutable.Map[Int,Char] = Map(1 -> c, 3 -> d)
scala> (f andThen g).isDefinedAt("b")
res3: Boolean = true
scala> (f andThen g).lift("b")
java.util.NoSuchElementException: key not found: 2
at scala.collection.MapLike$class.default(MapLike.scala:228)
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#import <Foundation/Foundation.h>
#import "ServerRequest.h" // works even though this line is included
#import "ServerResponseRecord.h"
@protocol ServerRequestDelegate<NSObject>
-(void)request:(id)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(id)request gotError:(NSError*)error;
@end
Run Code Online (Sandbox Code Playgroud)
它编译并运行良好.但是,如果我用以下方法替换方法声明:
-(void)request:(ServerRequest*)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(ServerRequest*)request gotError:(NSError*)error;
Run Code Online (Sandbox Code Playgroud)
我在'ServerRequest'之前得到意外的语法错误"error:expected')'.我认为这可能是一个问题的唯一原因是ServerRequestDelegate.h和ServerRequest.h #import彼此.但是,我不明白为什么代码适用于带有(id)请求的#import行.我也不明白为什么这是一个语法错误.
有人能提供一个很好的解释吗?
我有一个链接到YouTube视频的移动网站.在Android上,单击此链接会显示一个对话框,要求用户使用其浏览器或Youtube应用程序"完成操作".
有没有办法绕过这个屏幕,只是在Youtube应用程序中播放视频?(例如,使用youtube:// URL.)
谢谢!
我正在使用以下代码将一些输入传递给具有pexpect的进程:
p = pexpect.spawn('cat', timeout=5.0 )
p.maxread = 5000
p.setecho(False) # prevent the process from echoing stdin back to us
INPUT_LEN = 1024
p.sendline('a'*INPUT_LEN)
print p.readline() # pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
Run Code Online (Sandbox Code Playgroud)
当INPUT_LEN <1024时,一切正常,但是对于> = 1024个字符,进程没有收到完整输入,导致在p.readline()上引发"pexpect.TIMEOUT"错误.
我尝试将输入分成小于1024个字符的片段,但这有同样的问题:
p = pexpect.spawn('cat', timeout=5.0 )
p.maxread = 5000
p.setecho(False)
INPUT_LEN = 1024
p.send('a'*1000)
p.sendline('a'*(INPUT_LEN-1000))
print p.readline() # pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
Run Code Online (Sandbox Code Playgroud)
有谁知道如何使用超过1024个字符的输入使pexpect工作?我试着查看源代码,但它似乎只是在调用os.write(...).
(作为旁注,我注意到当我从shell运行"cat"并尝试使用"Cmd + V"粘贴> = 1024个字符时发生相同的截断错误.但是,如果我运行"pbpaste,一切正常|猫".)
谢谢!
更新: 对"os.write()"的调用返回1025,表示写入成功,但os.read()返回"\ x07"(单个字符BEL),然后在下一次调用时挂起,导致超时.
将os.write()调用分为两个1024字节的write(),由os.fsync()调用分隔,不会改变任何东西.
我想使用AVPlayer缓存渐进式下载视频.如何将AVPlayer的项目保存到磁盘?我正在尝试在播放器的currentItem(完全加载)上使用AVAssetExportSession.这段代码给了我" AVAssetExportSessionStatusFailed(操作无法完成) ":
AVAsset *mediaAsset = self.player.currentItem.asset;
AVAssetExportSession *es = [[AVAssetExportSession alloc] initWithAsset:mediaAsset presetName:AVAssetExportPresetLowQuality];
NSString *outPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"out.mp4"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:outPath error:NULL];
es.outputFileType = @"com.apple.quicktime-movie";
es.outputURL = [[[NSURL alloc] initFileURLWithPath:outPath] autorelease];
NSLog(@"exporting to %@",outPath);
[es exportAsynchronouslyWithCompletionHandler:^{
NSString *status = @"";
if( es.status == AVAssetExportSessionStatusUnknown ) status = @"AVAssetExportSessionStatusUnknown";
else if( es.status == AVAssetExportSessionStatusWaiting ) status = @"AVAssetExportSessionStatusWaiting";
else if( es.status == AVAssetExportSessionStatusExporting ) status = @"AVAssetExportSessionStatusExporting";
else if( es.status == AVAssetExportSessionStatusCompleted ) status = @"AVAssetExportSessionStatusCompleted"; …
Run Code Online (Sandbox Code Playgroud) 我正在设计一个简单的测验应用程序.应用程序需要显示不同类型的QuizQuestions.每种类型的QuizQuestion都有不同的行为和UI.
用户界面将是这样的: alt text http://dl.getdropbox.com/u/907284/Picture%201.png
我希望能够在Interface Builder中设计每种类型的QuizQuestion.
例如,MultipleChoiceQuizQuestion将如下所示: alt text http://dl.getdropbox.com/u/907284/Picture%202.png
最初,我计划将QuizQuestion类设为UIViewController.但是,我在Apple文档中读到UIViewControllers应该只用于显示整个页面.
因此,我制作了我的QuizController(管理整个屏幕,例如prev/next按钮)UIViewController,我的QuizQuestion类是UIView的子类.
但是,要加载这个UIView(在IB中创建),我必须[1]在我的构造函数中执行以下操作:
//MultipleQuizQuestion.m
+(id)createInstance {
UIViewController *useless = [[UIViewController alloc] initWithNibName:@"MultipleQuizQuestion" bundle:nil];
UIView *view = [[useless.view retain] autorelease];
[useless release];
return view; // probably has a memory leak or something
}
Run Code Online (Sandbox Code Playgroud)
这种类型的访问似乎不是标准的或面向对象的.这种类型的代码是正常/可接受的吗?或者我在设计的某个地方做出了糟糕的选择?
谢谢,
编辑(为了清楚起见):我想有一个单独的类来控制multipleChoiceView ...就像一个ViewController,但显然只适用于整个窗口.也许我应该创建一个MultipleChoiceViewManager(而不是控制器!)并将文件的所有者设置为它?
我有一个看起来像这样的课程:
@interface Properties : NSObject {
@private
NSNumber* prop1;
NSNumberBool* prop2;
//etc
Run Code Online (Sandbox Code Playgroud)
其中NSNumberBool是typedef:
// in MyApp_Prefix.pch
typedef NSNumber NSNumberBool;
Run Code Online (Sandbox Code Playgroud)
我有所有必需的@property和@synthesize声明来制作prop1和prop2属性.
所有编译和工作正常,直到我试图通过[myProperties valueForKey:@"prop2"]访问prop2.这给了我一个"类不符合键值"的错误.但是,许多类似的调用工作正常:
myProperties.prop2; //works
[myProperties prop2]; //works
[myProperties valueForKey:@"prop1"]; //works
[myProperties valueForKey:@"prop2"] // throws NSUnknownKeyException ??
Run Code Online (Sandbox Code Playgroud)
这里发生了什么,我该如何解决?
谢谢,
objective-c ×4
iphone ×3
python ×2
android ×1
avfoundation ×1
avplayer ×1
cocoa ×1
cocoa-touch ×1
expect ×1
gcc ×1
import ×1
io ×1
ios ×1
javascript ×1
macos ×1
maybe ×1
monads ×1
pexpect ×1
properties ×1
regex ×1
scala ×1
scope ×1
typedef ×1
uibutton ×1
uiview ×1
variables ×1
xcode ×1
youtube ×1