小编daf*_*afi的帖子

从其他模块调用的数组扩展

其他模块(例如XCTest项目)不提供数组扩展方法

为简单起见,下面的代码什么都不做,但它可以用来重现错误

import Foundation

extension Array {
    mutating func myMethod(toIndex: Int) -> Int! {
        // no real code, it's here only to show the problem
        return 0
    }
}
Run Code Online (Sandbox Code Playgroud)

从同一模块调用它可以按预期工作,但是从测试类中调用它不会

class MyProjectTests: XCTestCase {
    func testMoveObjectsFromIndexes1() {
        var arr = ["000", "001", "002", "003"]
        arr.myMethod(0)
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为这是正确的,因为方法可见性仅限于其自己的模块,实际上我获得了错误 '[String]' does not have a member named 'myMethod'

我试图定义扩展方法public,如下所示

extension Array {
    public mutating func myMethod(toIndex: Int) -> Int! {
        // no real code, it's here only to show the …
Run Code Online (Sandbox Code Playgroud)

swift

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

沙盒和目录列表

我做的一项常见任务是使用NSFileManager读取目录内容:contentsOfDirectoryAtPath但在Lion中启用沙盒这是不可能的.

我知道用户必须从NSOpenPanel中选择目录以使Sandbox满意(或放弃它)但是如何"通知"沙箱我必须从以前存储在某些配置文件中的路径读取目录而无需用户交互?

目前我已设置以下授权密钥

com.apple.security.files.user-selected.read-write
com.apple.security.documents.user-selected.read-write
com.apple.security.temporary-exception.files.absolute-path.read-write
Run Code Online (Sandbox Code Playgroud)

macos cocoa sandbox nsfilemanager osx-lion

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

Cocoa如何设置最大窗口高度但保留宽度

我有一个窗口,可以调整宽度,但有一个固定的高度,这听起来很容易,但我有一个我非常讨厌的硬编码值.

我的代码是

NSWindow* win = ...;
NSSize maxSize = [win maxSize];
maxSize.width = 30000;
[win setMaxSize: maxSize];
Run Code Online (Sandbox Code Playgroud)

如何编写此代码以使用系统默认值?

如果我的方法完全错误,我怎么能设置最大窗口的大小只有一个维度(宽度或高度)而另一个维度?

cocoa nswindow

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

复制到Snow Leopard或更高版本的粘贴板文件路径和URL

我想在剪贴板中复制文件路径,以便它们可以作为字符串在文本编辑器中复制,但我希望它们也可供Finder复制文件.

我编写了符合Snow Leopard准则的下面显示的代码(例如,在复制文件URL时使用writeObjects)

NSString* path1 = @"/Users/dave/trash/mas.sh";
NSString* path2 = @"/Users/dave/trash/books.xml";
NSURL* url1 = [NSURL fileURLWithPath:path1 isDirectory:NO];
NSURL* url2 = [NSURL fileURLWithPath:path2 isDirectory:NO];
NSArray* paths = [NSArray arrayWithObjects:path1, path2, nil];

NSString* pathPerLine = [paths componentsJoinedByString:@"\n"];
// Put strings on top otherwise paster app receives the url (only the first)
// Urls will be used by Finder for files operations (copy, move)
NSArray* urls = [NSArray arrayWithObjects:pathPerLine, url1, url2, nil];
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:urls];
Run Code Online (Sandbox Code Playgroud)

但是在一些编辑器(如XCode)上也会粘贴网址,如下所示(Finder正确使用网址进行复制/移动)

/Users/dave/trash/mas.sh …
Run Code Online (Sandbox Code Playgroud)

cocoa pasteboard osx-snow-leopard

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

将NSPopupButton调整为其选定的标题

我有一个NSPopupButton,我想要它调整自己以适应所选的标题.

[NSPopupButton sizeToFit]不符合我的需要,因为弹出窗口被调整为最大的标题项而不是当前选定的标题项

我试图在没有成功的情况下尽可能地接近

#define ARROW_WIDTH 20
NSDictionary *displayAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[popup font], NSFontAttributeName, nil];
NSSize titleSize = [popup.titleOfSelectedItem sizeWithAttributes:displayAttributes] + ARROW_WIDTH;
Run Code Online (Sandbox Code Playgroud)

但是常量值ARROW_WIDTH是一个非常脏且容易出错的解决方案.

状态栏上的TextWrangler编码组合就像我需要的那样工作

cocoa nspopupbuttoncell nspopupbutton

2
推荐指数
1
解决办法
1869
查看次数

NSAlert的响应按钮未在右侧对齐

我有一个带有附件视图和三个按钮的NSAlert,按钮未向右对齐(对于LTR系统),但似乎扩展到附件视图的宽度,如下所示 NSAlert和附件视图

同时删除附件视图,三个按钮在右侧未正确对齐

如何解决这个问题?我不想替换自己编写代码的NSAlert,为什么我必须重新发明轮子呢?

我使用的代码由setAccessoryView文档复制并在下面报告

NSTextView *accessory = [[NSTextView alloc] initWithFrame:NSMakeRect(0,0,600,15)];
NSFont *font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
NSDictionary *textAttributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
[accessory insertText:[[NSAttributedString alloc] initWithString:@"Text in accessory view"
                                                      attributes:textAttributes]];
[accessory setEditable:NO];
[accessory setDrawsBackground:NO];

NSAlert *alert = [[NSAlert alloc] init];

[alert setMessageText:@"Message text"];
[alert setInformativeText:@"Informative text"];
[alert setAccessoryView:accessory];

[alert addButtonWithTitle:@"Btn 3"];
[alert addButtonWithTitle:@"Btn 2"];
[alert addButtonWithTitle:@"Btn 1"];

[alert runModal];
[alert release];
Run Code Online (Sandbox Code Playgroud)

cocoa nsalert

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