小编Mar*_*rco的帖子

NSFileCoordinator正确使用沙盒Mac App Store应用程序中的sqlite日志文件

由于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是: …

sqlite cocoa objective-c ios nsfilecoordinator

6
推荐指数
0
解决办法
1304
查看次数

NSRulerView 如何将行号与正文正确对齐

我在 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)

macos cocoa objective-c nstextview

6
推荐指数
1
解决办法
676
查看次数

在Swift中访问全局变量

鉴于此示例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.

swift

5
推荐指数
1
解决办法
997
查看次数

有没有办法在C语言切换语句中使用指针?

通常在动态类型编程语言中,对象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语句系列.

c compiler-construction interpreter vm-implementation

4
推荐指数
1
解决办法
166
查看次数

向上翻n/4的有效方法

我有一个整数n,我需要向上舍入n/4.出于性能原因,我需要在C中找到一个快速的方法.除以4可以使用>> 2移位操作来完成,但我不知道该轮.我可以使用ceil,但我担心性能.

c algorithm math optimization rounding

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

优化C中的位解码操作

我有一个无符号的32位整数,按以下方式编码:

  • 前6位定义了 opcode
  • 接下来的8位定义了一个 register
  • 接下来的18位是二进制补码有符号整数value.

我正在使用以下方法解码此数字(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 performance bit-manipulation

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