小编Ben*_*n H的帖子

OCMock方法名称冲突

我是OCMock的新用户,所以也许我只是在这里错过了一些简单的东西.此代码无法编译:

id mockSession = [OCMockObject mockForClass:[AVCaptureSession class]];
[[mockSession expect]  addOutput:[OCMArg anyPointer]];
Run Code Online (Sandbox Code Playgroud)

错误是

Multiple methods named 'addOutput:' found with mismatched result, parameter type or attributes
Run Code Online (Sandbox Code Playgroud)

AVCaptureSession上方法addOutput的签名如下

- (void)addOutput:(AVCaptureOutput *)output
Run Code Online (Sandbox Code Playgroud)

据我所知,问题是方法addOutput存在于AVCaptureSession和AVAssetReader类中.AVAssetReader上addOutput的方法签名如下.

- (void)addOutput:(AVAssetReaderOutput *)output
Run Code Online (Sandbox Code Playgroud)

显然编译器认为我的mockSession是一个AVAssetReader,但我不知道为什么它选择该类而不是AVCaptureSession.如果我期望AVCaptureSession上的另一种方法在AVAssetReader上不存在,那么它就会编译.我试过以下没有成功.它编译,但崩溃.

id mockSession = [OCMockObject mockForClass:[AVCaptureSession class]];
[(AVCaptureSession*)[mockSession expect]  addOutput:[OCMArg anyPointer]];
Run Code Online (Sandbox Code Playgroud)

此代码也不编译,与前一个错误相同

id mockSession = [OCMockObject mockForClass:[AVCaptureSession class]];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[[mockSession expect]  addOutput:output];
Run Code Online (Sandbox Code Playgroud)

这里的任何指导?

unit-testing objective-c ocmock ios

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

Objective-C:如何在JSON字典中放置布尔值?

我无法找到如何key:true在我的NSDictionary中插入一个布尔值(在JSON字符串中显示):

NSMutableDictionary* jsonDict = [NSMutableDictionary dictionary];
[jsonDict setValue: YES forKey: @"key"];
Run Code Online (Sandbox Code Playgroud)

上面的代码没有运行(显然因为YES不是对象).
我怎么能做到这一点?

macos cocoa json objective-c

7
推荐指数
3
解决办法
9526
查看次数

通过NSSecureCoding解码NSArray时的奇怪行为

我花了整个下午的时间撞到墙上,试图弄清楚为什么这门课的解码失败了.该类有一个属性,它是Foo对象的NSArray.Foo符合NSSecureCoding,我已成功编码并解码了该类.我在initWithCoder中收到错误:表示无法解码类Foo.通过一些实验,我发现我需要将[Foo类]添加到initWithCoder:为了使它工作.也许这会帮助那些遇到同样问题的人.我的问题是,为什么这是必要的?我没有发现在Apple的文档中这是必要的.

#import "Foo.h"

@interface MyClass : NSObject <NSSecureCoding>
@property (nonatomic) NSArray *bunchOfFoos;
@end

@implementation MyClass

static NSString *kKeyFoo = @"kKeyFoo";

+ (BOOL) supportsSecureCoding
{
    return YES;
}

- (void) encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.bunchOfFoos forKey:kKeyFoo];
}

- (id) initWithCoder:(NSCoder *)decoder
{
    if (self = [super init])
    {
        [Foo class]; // Without this, decoding fails
        _bunchOfFoos = [decoder decodeObjectOfClass:[NSArray class] forKey:kKeyFoo];
    }
    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

objective-c nscoding nsarray ios nssecurecoding

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

为什么我以编程方式创建的视图会忽略其约束?

我有一个视图,以编程方式创建子视图并为其添加一些约束.出于某种原因,约束被完全忽略.不会抛出任何错误.约束根本不起作用.子视图与超级视图具有相同的框架.我期待它是400x400并以其超级视图为中心.我在这做错了什么?这是我在superview中的initWithFrame:

- (id) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        instructionsView = [[UIView alloc] initWithFrame:frame];
        [self addSubview:instructionsView];
        [self bringSubviewToFront:instructionsView];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                         attribute:NSLayoutAttributeCenterX
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterX
                                                        multiplier:1.
                                                          constant:0]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                         attribute:NSLayoutAttributeCenterY
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterY
                                                        multiplier:1.
                                                          constant:0]];

        [instructionsView addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                                     attribute:NSLayoutAttributeWidth
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:nil
                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                    multiplier:1.
                                                                      constant:400]];

        [instructionsView addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                                     attribute:NSLayoutAttributeHeight
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:nil
                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                    multiplier:1.
                                                                      constant:400]];
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

我试过改变

instructionsView = [[UIView alloc] initWithFrame:frame];
Run Code Online (Sandbox Code Playgroud)

instructionsView = [[UIView alloc] initWithFrame:CGRectZero];
Run Code Online (Sandbox Code Playgroud)

没有成功.

objective-c ios autolayout

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

如何在 Win32 桌面应用程序中使用自定义 WinRT 库?

我的基于 Win32 的桌面应用程序中有一个 WinRT 类(C++/CX 引用类)。它访问 WinRT API 并且工作得很好。我使用本指南来让它工作。现在我试图将此类放入桌面应用程序可以使用的库中。我在这方面遇到了一些麻烦。这是我在 Visual Studio 2013 中所做的:

  1. 通过选择“已安装”>“模板”>“Visual C++”>“商店应用程序”>“Windows 应用程序”>“DLL (Windows)”创建了一个新项目。
  2. 将此新的 DLL 项目添加到包含我的桌面应用程序的解决方案中。
  3. 添加了对 DLL 项目的引用
  4. “配置属性”>“C/C++”>“常规”>“其他 #using 目录”中,我添加了 DLL 项目构建 .winmd 文件的目录。
  5. 在桌面应用程序的 .cpp 文件中,我添加了:
#using <MyLib.winmd>
#using <Windows.winmd>
#using <Platform.winmd>

[MTAThread] // initializes WinRT runtime
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow) {
    MyWinRTClass^ myObject = ref new MyWinRTClass();
}
Run Code Online (Sandbox Code Playgroud)

智能感知可以工作,我编写了代码来实例化库中的类。桌面应用程序已构建,但当它运行时我得到:

MyDesktopApp.exe 中 0x76494598 处的首次机会异常:Microsoft C++ 异常:Platform::ClassNotRegisteredException ^ …

dll windows-8 windows-runtime c++-cx

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

为什么CGContextAddArc可以在32位而不是64位设备上运行?

我在图层委托中有以下代码.它只是绘制一个蓝色圆圈.它在32位iOS设备上运行完美,但在64位iOS设备上没有任何效果.为什么?

-(void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    CGContextAddArc(context, 100., 100., 10., -M_PI, M_PI, YES);
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextFillPath(context);
}
Run Code Online (Sandbox Code Playgroud)

drawing core-graphics objective-c ios

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