由于sqlite执行事务的方式(使用wal或journal文件),我有很多麻烦将我的sqlite代码移植到App Store.Apple文档的相关部分是:
您的应用程序需要能够打开或保存具有相同名称和不同扩展名的多个相关文件(例如,自动打开与电影文件同名的字幕文件,或允许SQLite日志文件).要访问该辅助文件,请创建符合NSFilePresenter协议的类.此对象应将主文件的URL作为其primaryPresentedItemURL属性提供,并应将辅助文件的URL作为其presentItemURL属性提供.用户打开主文件后,文件presenter对象应调用NSFileCoordinator类上的addFilePresenter:class方法来注册自己.
Apple DTS为我提供了以下代码:
- (void)openSQLiteFileAtURL:(NSURL *)fileURL {
NSFileCoordinator *fc = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fc coordinateReadingItemAtURL:fileURL options:0 error:NULL byAccessor:^(NSURL *newURL) {
sqlite3 *db = NULL;
char *zErrMsg = 0;
[SQLiteRelatedItemPresenter addPresentersForURL:newURL];
int rc = sqlite3_open_v2([[newURL path] fileSystemRepresentation], &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
NSLog(@"open %@ = %d", [newURL path], rc);
rc = sqlite3_exec(db, "CREATE TABLE foo (col1 INTEGER);", callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
NSLog(@"SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
// more sqlite code here
sqlite3_close(db);
}];
return;
}
Run Code Online (Sandbox Code Playgroud)
其中SQLiteRelatedItemPresenter是: …
我在 MacOS 中使用 NSRulerView 以便在 NSTextView 旁边显示行号。两个视图共享相同的字体和相同的字体大小,但是在 NSTextView 中,字符串呈现是自动管理的,在 NSRulerView 中,我需要计算正确的行号(这部分工作正常),然后在 drawHashMarksAndLabelsInRect 中呈现字符串。
我的问题是我无法在两个视图之间正确对齐文本。对于某些字体,它可以正常工作,而对于其他字体,则存在明显差异。
我实际使用的代码是:
#define BTF_RULER_WIDTH 40.0f
#define BTF_RULER_PADDING 5.0f
static inline void drawLineNumber(NSUInteger lineNumber, CGFloat y, NSDictionary *attributes, CGFloat ruleThickness) {
NSString *string = [[NSNumber numberWithUnsignedInteger:lineNumber] stringValue];
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
NSUInteger x = ruleThickness - BTF_RULER_PADDING - attString.size.width;
[attString drawAtPoint:NSMakePoint(x, y)];
}
static inline NSUInteger countNewLines(NSString *s, NSUInteger location, NSUInteger length) {
CFStringInlineBuffer inlineBuffer;
CFStringInitInlineBuffer((__bridge CFStringRef)s, &inlineBuffer, CFRangeMake(location, length));
NSUInteger counter = 0;
for …Run Code Online (Sandbox Code Playgroud) 鉴于此示例Swift代码:
var a = 10;
func foo() -> Int {
var a = 20;
return a;
}
Run Code Online (Sandbox Code Playgroud)
函数foo如何访问值为10的全局变量a而不是值为20的本地a?
请注意,a和foo都不是在类中声明,而是在通用模块中声明.我正在寻找一种方法来告诉Swift访问全局定义的var而不是本地定义的var.
通常在动态类型编程语言中,对象struct具有用于标识对象类型的标记字段.例如:
struct myObject {
int tag;
...
}
Run Code Online (Sandbox Code Playgroud)
因此,使用基于标记字段的switch语句可以轻松执行不同的操作.例如:
switch (obj->tag) {
case OBJ_INTEGER: ...
case OBJ_STRING: ...
case OBJ_FUNC:...
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我使用了一个void*isa指针而不是int标签字段,该指针指向代表该对象的类.一切正常,我不得不使用优雅的switch语句而不是使用一系列if/else语句.例如:
if (obj->isa == class_integer) {
...
} else if (obj->isa == class_string) {
...
} else if (obj->isa == class_func) {
...
}
Run Code Online (Sandbox Code Playgroud)
我知道我不能在C开关语句中使用指针,但我想知道我是否可以使用一些聪明的技巧来加速if语句系列.
我有一个整数n,我需要向上舍入n/4.出于性能原因,我需要在C中找到一个快速的方法.除以4可以使用>> 2移位操作来完成,但我不知道该轮.我可以使用ceil,但我担心性能.
我有一个无符号的32位整数,按以下方式编码:
opcoderegistervalue.我正在使用以下方法解码此数字(uint32_t inst):
const uint32_t opcode = ((inst >> 26) & 0x3F);
const uint32_t r1 = (inst >> 18) & 0xFF;
const int32_t value = ((inst >> 17) & 0x01) ? -(131072 - (inst & 0x1FFFF)) : (inst & 0x1FFFF);
Run Code Online (Sandbox Code Playgroud)
我可以在解码值时测量显着的开销,我很确定这是由于三元运算符(基本上是if语句)用于比较符号执行否定操作.
有没有办法以更快的方式执行值解码?
c ×3
cocoa ×2
objective-c ×2
algorithm ×1
interpreter ×1
ios ×1
macos ×1
math ×1
nstextview ×1
optimization ×1
performance ×1
rounding ×1
sqlite ×1
swift ×1