小编joa*_*kim的帖子

XCTAssertEqual用于Swift中的自定义对象

XCode 6,Beta 5

我有这样的单元测试:


    func testMyObjectsEqual()
    {
        //....

        XCTAssertEqual(myObject, myOtherObject, "\(myObject) and \(myOtherObject) should be equal")
    }

XCTAssertEqualObjects在Swift中不再可用,因为该语言不区分标量和对象.

所以我们必须使用XCTAssertEqual,这会导致以下错误:

 "Type MyObject does not conform to protocol Equatable"

我发现的唯一解决方法是从NSObject继承(MyObject),以便我可以执行以下操作:

 XCTAssert(myObject == myOtherObject, "\(myObject) and \(myOtherObject) should be equal")

所以我的问题是:有没有办法(从beta 5开始)将XCTAssertEqual用于自定义类型而不必依赖NSObject或乱丢所有带有"=="重载的自定义类型?

unit-testing ios swift

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

iOS 7中UIImageview的新tintColor属性可以用于动画图像吗?

tintColor是一个救生员,它需要应用主题到一个全新的(简单)级别.

//the life saving bit is the new UIImageRenderingModeAlwaysTemplate mode of UIImage
UIImage *templateImage = [[UIImage imageNamed:@"myTemplateImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.image = templateImage;

//set the desired tintColor
imageView.tintColor = color;
Run Code Online (Sandbox Code Playgroud)

上面的代码将根据UIImageview的色调颜色"绘制"图像的非透明部分,这非常酷.不需要像这样简单的核心图形.

我面临的问题是动画.

继续上述代码:

//The array with the names of the images we want to animate
NSArray *imageNames = @[@"1",@"2"@"3"@"4"@"5"];

//The array with the actual images
NSMutableArray *images = [NSMutableArray new];
for (int i = 0; i < imageNames.count; i++)
{
    [images addObject:[[UIImage imageNamed:[imageNames objectAtIndex:i]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
}

//We set the animation images of …
Run Code Online (Sandbox Code Playgroud)

animation objective-c uiimageview tintcolor ios

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

AWS IAM 基于用户的策略 vs 基于资源的策略 vs 两者

让我们假设一个基于用户的IAM 策略,即可以附加到用户、组或角色的IAM 策略。

假设一个可以完全访问 DynamoDB 表的程序:

{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": "dynamodb:*",
    "Resource": "arn:aws:dynamodb:us-west-2:123456789:table/Books"
  }
}
Run Code Online (Sandbox Code Playgroud)

基于此策略,任何以某种方式最终将策略附加到他们的用户(例如通过承担角色或直接)都可以获得对该 DynamoDB 表的完全访问权限。

问题 1:是否值得在另一端(即 DynamoDB 表)制定基于资源的策略来补充基于用户的策略?

例子:

{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Principal": {"AWS": "arn:aws:iam::123456789012:user/bob"},
    "Action": "dynamodb:*",
    "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/Books"
}
Run Code Online (Sandbox Code Playgroud)

这里的动机是,先前的策略可能最终会意外地附加到某人,而使用基于资源的策略将确保只有用户Bob将被授予这些权限。

问题 2:使用更严格的资源策略是否 只是更好?

问题3:一般情况下,是否有采摘之间任何的最佳做法/模式基于用户的VS基于资源的策略(即支持服务基于资源的政策是)?

amazon-web-services amazon-iam

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

在Swift中将协议类型作为参数传递

在objective-C中,我们可以(通过导入语言的运行时头文件)执行以下操作:

//Pass a service (usually an object) and ANY protocol
- (void)registerService:(id)service forProtocol:(Protocol *)protocol
{
    //Grab the protocol's name (that's why we import runtime.h, it contains the protocol_getname mehod)
    NSString *protocolName = [NSString stringWithUTF8String:protocol_getName(protocol)];

    //If the object we passed does not conform to the protocol, inform and break
    if (![service conformsToProtocol:protocol])
    {
        NSLog(@"Service: %@ does not conform to protocol: %@", service, protocolName);
        return;
    }

    //Else add service in a collection (array, dictionary) for later use
    self.services[protocolName] = service;
}
Run Code Online (Sandbox Code Playgroud)

我在obj-C中使用它作为"穷人的IOC容器",一个用于注入依赖项的简单注册表. …

protocols ios swift

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