小编NSA*_*ict的帖子

单线闭合,无返回类型

在Swift中,如果闭包只保存一个语句,它会自动返回从该单个语句返回的值.

在所有情况下,这都不是很自然.我们来看一个例子:

func StringReturningFunc() -> String {
    return "Test String"
}

// Error: Cannot convert the expressions type '() -> $T0' to type 'String'
let closure: () -> () = {
    StringReturningFunc()
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,即使闭包只应调用一个简单的函数,它也会尝试自动返回它的返回值,该值是类型String,并且与返回类型不匹配void.

我可以通过实现这样的闭包体来防止这种情况:

let _ = StringReturningFunc()
Run Code Online (Sandbox Code Playgroud)

这感觉非常奇怪.

有没有更好的方法来做到这一点,或者这只是我必须忍受的东西?

swift

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

获取宽度的现有NSLayoutConstraint?

我正在尝试使用自动布局为Cocoa中的控件设置动画.

现在,我可以设置[[constraint animator] setConstant:newWidth];,哪个有效.但是我怎样才能得到正确的约束呢?

有了[self constraints]你可以得到所有的约束,在这种情况下我可以选择constraints[0],但限制的顺序可能会有所不同.

我怎么能确定我总是有正确的约束?约束在Interface Builder中设置.我已经看到你可以添加一个IBOutlet,但它似乎没有必要.


我的解决方案

谢谢,它运作得很好.我写了一个小类.


的NSView + NSLayoutConstraintFilter.h

#import <Cocoa/Cocoa.h>

@interface NSView (NSLayoutConstraintFilter)
- (NSLayoutConstraint *)constraintForAttribute:(NSLayoutAttribute)attribute;
- (NSArray *)constaintsForAttribute:(NSLayoutAttribute)attribute;
@end
Run Code Online (Sandbox Code Playgroud)

的NSView + NSLayoutConstraintFilter.m

#import "NSView+NSLayoutConstraintFilter.h"

@implementation NSView (NSLayoutConstraintFilter)

- (NSArray *)constaintsForAttribute:(NSLayoutAttribute)attribute {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", attribute];
    NSArray *filteredArray = [[self constraints] filteredArrayUsingPredicate:predicate];

    return filteredArray;
}

- (NSLayoutConstraint *)constraintForAttribute:(NSLayoutAttribute)attribute {
    NSArray *constraints = [self constaintsForAttribute:attribute];

    if (constraints.count) {
        return constraints[0];
    }

    return nil;
}

@end
Run Code Online (Sandbox Code Playgroud)

macos xcode cocoa objective-c autolayout

17
推荐指数
2
解决办法
9529
查看次数

自动调整NSTableView的高度

我之前曾问过这个问题,但我对解决方案不太满意.

自动调整NSTableView的大小

我想NSTableView在一个NSPopover或一个中显示一个NSWindow.
现在,窗口的大小应该根据表格视图进行调整.

就像Xcode一样:


在此输入图像描述 在此输入图像描述


使用自动布局时,这非常简单,您可以将内部视图固定到超级视图.

我的问题是,我无法弄清楚表格视图的最佳高度.以下代码枚举所有可用行,但它不返回正确的值,因为表视图具有其他元素,如分隔符和表头.

- (CGFloat)heightOfAllRows:(NSTableView *)tableView {
    CGFloat __block height;
    [tableView enumerateAvailableRowViewsUsingBlock:^(NSTableRowView *rowView, NSInteger row) {
        // tried it with this one
        height += rowView.frame.size.height;

        // and this one
        // height += [self tableView:nil heightOfRow:row];
    }];

    return height;
}
Run Code Online (Sandbox Code Playgroud)

1.问题

我怎样才能解决这个问题?如何正确计算表格视图所需的高度.

2.问题

我应该在哪里运行此代码?
我不想在控制器中实现它,因为它肯定是表视图应该自己处理的东西.
我甚至没有找到任何有用的委托方法.

所以我认为最好是你可以继承NSTableView.
那么我的问题2,在哪里实现它?


动机

绝对值得赏心悦目

macos cocoa objective-c nstableview autolayout

17
推荐指数
3
解决办法
8514
查看次数

Objective C keypath从iTunes中获取所有艺术家

我正在使用键值编码来获取iTunes中的所有艺术家:

[self.iTunes valueForKeyPath:@"sources.@distinctUnionOfArrays.playlists.@distinctUnionOfArrays.tracks.artist"];
Run Code Online (Sandbox Code Playgroud)

现在,这很好用.这非常有效.我也想对专辑做同样的事情.

[self.iTunes valueForKeyPath:@"sources.@distinctUnionOfArrays.playlists.@distinctUnionOfArrays.tracks.album"];
Run Code Online (Sandbox Code Playgroud)

这里的问题是,有多个相同名称的专辑,但不一定是同一个艺术家.有没有办法获得每张专辑的歌曲,所以我可以找出它是什么艺术家,并获得它的封面?我知道有NSPredicate,但这很慢.

具体代码并不重要,我只需要键值编码部分.

谢谢!

enumeration objective-c key-value-coding nspredicate

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

通过代码添加和设置NSLayoutConstraint

我遇到了一些麻烦NSLayoutConstraint.

如果我尝试constant使用该setConstant:方法进行更改,NSLayoutConstraint会给我一个例外.当我通过代码添加高度约束时,我只有这个问题.

首先,我得到高度限制,如下:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", NSLayoutAttributeWidth];
NSArray *filteredArray = [[self constraints] filteredArrayUsingPredicate:predicate];
return filteredArray[0];
Run Code Online (Sandbox Code Playgroud)

这给了我正确的约束.我有一个NSTextField子类,这是完美的.约束在Interface Builder中设置,我可以设置和更改常量.

现在我有一个视图,我在运行时添加不同的子视图.这些子视图位于自己的NIB中,这意味着我无法固定它们的宽度和高度.所以我认为一旦将视图添加到superview,我就会添加约束.这是在执行viewDidMoveToSuperview.

// Pin Width & Height
[self addConstraint:[NSLayoutConstraint constraintWithItem:self
                             attribute:NSLayoutAttributeWidth
                             relatedBy:NSLayoutRelationEqual
                                toItem:nil
                             attribute:NSLayoutAttributeNotAnAttribute
                            multiplier:1.0f
                              constant:self.frame.size.width]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                 attribute:NSLayoutAttributeHeight
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:nil
                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                multiplier:1.0f
                                                  constant:self.frame.size.height]];
Run Code Online (Sandbox Code Playgroud)

添加了约束,我可以使用NSLog确认.

"<NSLayoutConstraint:0x100605cc0 H:[ITControlPanelChildView:0x100616eb0(269)]>",
"<NSLayoutConstraint:0x100614410 V:[ITControlPanelChildView:0x100616eb0(317)]>"
Run Code Online (Sandbox Code Playgroud)

现在,最后,当我尝试使用[constraint setConstant:somethingDifferent];I 来改变约束时,我得到以下异常:

Unable to simultaneously satisfy constraints:
(
    "<NSLayoutConstraint:0x10192fcb0 V:[ITControlPanelChildView:0x10192e4f0(100)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x10053ff10 h=--& v=&-- V:[ITControlPanelChildView:0x10192e4f0(317)]>"
)

Will attempt to recover …
Run Code Online (Sandbox Code Playgroud)

macos cocoa objective-c autolayout nslayoutconstraint

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

拍照,在相机中选择全模式或方模式

有没有办法使用叠加UIImagePickerController来显示用户可能使用的方形图片,同时在那里有一个切换按钮可以立即切换?

目前iOS 7相机具有此功能,但UIImagePickerController不支持(感谢Apple),那么有没有办法添加此功能?

请问使用AVCaptureSession是必要的?这似乎是严重的矫枉过正,我认为我必须重新编程flash/focus/etc.

如果失败了,是否存在一个我可以实现的可定制类?

我正在撞墙,试图弄清楚这里最好的行动方案,所以任何帮助都会受到赞赏.

image-processing objective-c uiimagepickercontroller ios

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

在iOS 7中展示UIImagePickerController的问题

我的应用程序仅以横向模式运行!所以我UIImagePickerController只知道纵向模式下的礼物,所以在iOS 6中,我创建了一个UIImagePickerController强制UIImagePickerController以纵向模式打开的子类:

@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

@implementation NonRotatingUIImagePickerController

- (BOOL)shouldAutorotate {

    return NO;
}

@end
Run Code Online (Sandbox Code Playgroud)
//presenting picker controller :

UIImagePickerController *ipc = [[NonRotatingUIImagePickerController alloc]init];
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

这在iOS 6中运行良好,但现在在iOS 7中,我的应用程序因此崩溃:

2013-10-31 14:56:01.028 Medad[1731:60b] *** Terminating app due to
uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason:
'Supported orientations has no common orientation with the
application, and shouldAutorotate is returning YES'
Run Code Online (Sandbox Code Playgroud)

如果我检查Portrait部署信息,可以解决此问题: 在此输入图像描述

问题是,如果我检查此选项,我的应用程序也会以纵向运行,但我不想要它!

我该如何解决这个问题?

iphone xcode objective-c ipad ios

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

使用iTunes的Up Up功能和API

有没有办法使用iTunes的Up Up功能和API?

我知道您可以使用Scripting Bridge,但它不包括iTunes 11的任何新功能.

macos xcode cocoa itunes objective-c

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

CATransition推动不褪色

我正在为Mac OS X制作导航控制器类.

我想用kCATransitionPush动画替换当前视图.
喜欢这篇文章:
核心动画教程 - 带转换的向导对话框

CATransition设置是这样的:

CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[self.view setAnimations:@{ @"subviews" : transition }];
Run Code Online (Sandbox Code Playgroud)

但是,当我替换视图时,会有一个淡入淡出的动画,它会被自动添加.

[NSAnimationContext beginGrouping];
{
    [[self.view animator] replaceSubview:_currentViewController.view with:newViewController.view];
}
[NSAnimationContext endGrouping];
Run Code Online (Sandbox Code Playgroud)

如何在没有褪色的情况下进行推动动画?

macos cocoa core-animation objective-c

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

目标c访问公共方法

我尝试从另一个类访问公共方法.我已经尝试了很多我在网络上找到的例子,但它们并没有按照我希望的方式工作.

Class1.h

@interface anything : NSObject {

    IBOutlet NSTextField *label;

}

+ (void) setLabel:(NSString *)string;
- (void) changeLabel:(NSString *)string2;
Run Code Online (Sandbox Code Playgroud)

Class1.m

+ (void) setLabel:(NSString *)string {

    Class1 *myClass1 = [[Class1 alloc] init];

    [myClass1 changeLabel:string];
    NSLog(@"setLabel called with string: %@", string);

}

- (void) changeLabel:(NSString *)string2 {

    [label setStringValue:string2];
    NSLog(@"changeLabel called with string: %@", string2);
}
Run Code Online (Sandbox Code Playgroud)

Class2.m

- (IBAction)buttonPressed {

    [Class1 setLabel:@"Test"];

}
Run Code Online (Sandbox Code Playgroud)

非常奇怪的是,在NSLogs中,一切都很好,在两个NSLog中,字符串都是"Test",但textField的stringValue不会改变!

cocoa objective-c nstextfield public-method

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