小编Mat*_*ler的帖子

Swift 2:尝试/捕捉非投掷功能

我正在重构一个Obj-c类,其中有一个@try/@catchremoveObserver:.

在swift中执行相同的操作会触发警告,因为removeObserver可能会失败(当没有观察者时),但它不会引发任何错误.

知道如何实现相同的行为吗?

编辑:我的代码:

try {  
    self.removeObserver(self, forKeyPath: "LineDisplayChanged")
}
Run Code Online (Sandbox Code Playgroud)

try-catch swift swift2

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

是否有一种增加可选Int的漂亮方法?

我想增加一个Int?
目前我写的这个:

return index != nil ? index!+1 : nil
Run Code Online (Sandbox Code Playgroud)

有没有更漂亮的方式来写这个?

int increment optional swift

11
推荐指数
2
解决办法
1047
查看次数

CGContextSaveGState:App Launch上的无效上下文0x0

我已设置CG_CONTEXT_SHOW_BACKTRACEenvironnement变量,YES因为我有以下错误CGContextSaveGState: invalid context 0x0.

以下堆栈跟踪对我没什么帮助.

<Error>: CGContextSaveGState: invalid context 0x0. Backtrace:
      <-[UIStatusBarBatteryItemView contentsImage]+773>
       <-[UIStatusBarItemView updateContentsAndWidth]+36>
        <-[UIStatusBarItemView initWithItem:data:actions:style:]+503>
         <+[UIStatusBarItemView createViewForItem:withData:actions:foregroundStyle:]+159>
          <-[UIStatusBarLayoutManager _createViewForItem:withData:actions:]+168>
           <-[UIStatusBarLayoutManager _prepareEnabledItemType:withEnabledItems:withData:actions:itemAppearing:itemDisappearing:]+3
            <-[UIStatusBarLayoutManager prepareEnabledItems:withData:actions:]+108>
             <-[UIStatusBarForegroundView _setStatusBarData:actions:animated:]+951>
              <-[UIStatusBarForegroundView setStatusBarData:actions:animated:]+874>
               <__51-[UIStatusBar _prepareToSetStyle:animation:forced:]_block_invoke+443>
                <+[UIView(Animation) performWithoutAnimation:]+82>
                 <-[UIStatusBar _prepareToSetStyle:animation:forced:]+935>
                  <-[UIStatusBar _requestStyleAttributes:animationParameters:forced:]+404>
                   <-[UIStatusBar requestStyle:animationParameters:forced:]+490>
                    <-[UIStatusBar requestStyle:animated:forced:]+113>
                     <-[UIStatusBar forceUpdateStyleOverrides:]+97>
                      <-[UIStatusBar _evaluateServerRegistration]+294>
                       <-[UIStatusBar didMoveToSuperview]+33>
                        <__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke+683>
                         <-[UIView(Hierarchy) _postMovedFromSuperview:]+484>
                          <-[UIView(Internal) _addSubview:positioned:relativeTo:]+2305>
                           <-[UIView(Hierarchy) addSubview:]+56>
                            <-[UIStatusBarWindow setStatusBar:]+311>
                             <-[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:]+384>
                              <-[UIApplication _runWithMainScene:transitionContext:completion:]+1047>
                               <__84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invo
                                <-[UIApplication workspaceDidEndTransaction:]+163>
                                 <__37-[FBSWorkspace clientEndTransaction:]_block_invoke_2+71>
                                  <__40-[FBSWorkspace _performDelegateCallOut:]_block_invoke+54>
                                   <-[FBSSerialQueue _performNext]+184>
                                    <-[FBSSerialQueue _performNextFromRunLoopSource]+52>
                                     <FBSSerialQueueRunLoopSourceHandler+33> …
Run Code Online (Sandbox Code Playgroud)

xcode core-graphics ios swift ios9

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

Swift 3:覆盖BecomeFirstResponder时出现"未使用的调用结果"

UIView子类中,我是覆盖BecomeFirstResponder(返回一个Bool.

class MyViewSubclass:UIView {
 ...
    override func becomeFirstResponder() -> Bool {
        // some stuff
        return super.becomeFirstResponder()
    }
 ...
}
Run Code Online (Sandbox Code Playgroud)

在子类的对象上调用此方法时,我收到警告Result of call to 'becomeFirstResponder()' is Unused.

当我不覆盖时,不会发生此消息becomeFirstResponder.

我知道,与斯威夫特3的方法返回结果已经演变行为(见@discardableResultSE-0047),但我希望一个覆盖有相同的行为,其超强的方法.

我错了认为或者它可能是编译器错误?

编辑:
仅供参考,为此提交了一个错误.

ios swift3

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

Java swing setMaximumSize不起作用

可能重复:
setMaximumSize在java中不起作用

我的JFrame子类遇到了问题.我需要设置最大高度.

但是setMaximumSize不起作用.这似乎是java中的一个错误(setMinimumSize工作).如何防止a JFrame高于给定高度?

java swing jframe

8
推荐指数
2
解决办法
2万
查看次数

选择QGraphicsItem时调用什么方法

可能是一个技巧问题,但我找不到答案.

我需要知道何时选择QGraphicsItem.必须有一个被调用的方法.

我知道,QGraphicsItem::itemChange()但它经常被称为.

有更好的方法吗?

谢谢

编辑:有了这个

if(change == ItemSelectedChange && scene()){
    cout << "haha " << i++ << endl;
}
Run Code Online (Sandbox Code Playgroud)

每次选择更改都会收到两个电话.

qt qgraphicsitem

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

使用Grand Central Dispatch进行文件监控

我正在使用David Hamrick的代码示例来监视使用GCD的文件.

int fildes = open("/path/to/config.plist", O_RDONLY);

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,fildes, 
                                                  DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE,
                                                  queue);
dispatch_source_set_event_handler(source, ^
{
    //Reload the config file
});
dispatch_source_set_cancel_handler(source, ^
{
    //Handle the cancel
});
dispatch_resume(source);
Run Code Online (Sandbox Code Playgroud)

我想用来监视一个plist的变化.我在第一次更改后收到通知,但没有收到以下更改.为什么?

cocoa objective-c grand-central-dispatch

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

为什么Segment.io loader脚本将方法名称/ args推送到看似被覆盖的队列?

我一直在剖析以下代码片段,它用于异步加载Segment.io分析包装器脚本:

// Create a queue, but don't obliterate an existing one!
var analytics = analytics || [];

// Define a method that will asynchronously load analytics.js from our CDN.
analytics.load = function(apiKey) {

    // Create an async script element for analytics.js.
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.src = ('https:' === document.location.protocol ? 'https://' : 'http://') +
                  'd2dq2ahtl5zl1z.cloudfront.net/analytics.js/v1/' + apiKey + '/analytics.min.js';

    // Find the first script element on the page and insert our …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous segment-io

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

使用iOS模拟器在辅助功能检查器中看不到辅助功能标识符

我想使用辅助功能检查器来验证我在模拟器中运行的应用程序中的所有辅助功能标识符(iOS 9.2).

辅助功能检查器能够返回多个辅助功能字段,但不能返回标识符.

知道为什么以及如何看到它们?

macos accessibility ios ios-simulator accessibility-inspector

8
推荐指数
3
解决办法
5663
查看次数

CoreData关系返回_NSCoreDataTaggedObjectID

我通过以下关系访问核心数据对象:

pArret.zzone?.libelle
Run Code Online (Sandbox Code Playgroud)

第一个对象与第二个对象具有To-one关系,并且libelle是第二个对象的属性.

我收到以下错误: -[_NSCoreDataTaggedObjectID libelle]: unrecognized selector sent to instance 0xd00000000e30000c.我不知道这个私有_NSCoreDataTaggedObjectID类是什么,为什么我没有NSManagedObject按预期获得我的子类?

core-data nsmanagedobject ios

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