小编hpi*_*que的帖子

嘲笑斯威夫特

你如何在Swift中模拟一个对象?

Mirror协议听起来很有希望,但它现在做的并不多.

到目前为止,我发现的唯一方法是子类化并覆盖模拟类的所有方法.这当然不是真正的模仿,远非理想,还有很多工作.

还有其他想法吗?

为什么不是OCMock?

来源:

我可以使用语言桥接功能使用OCMock吗?

是的,但有局限性.如果你很勇敢.截至目前,这是高度实验性的.无法保证OCMock将完全支持Swift.

已知限制:

  • 测试必须用Objective-C编写
  • 应该模拟的对象必须从NSObject继承
  • 没有存根/期望/验证类方法

unit-testing mocking swift

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

在Eclipse中将现有项目转换为Android项目?

如何将现有项目转换为Eclipse中的Android项目?

特别是,我想将一个普通的旧Java项目转换为Android库项目.

谢谢.

eclipse android

51
推荐指数
3
解决办法
4万
查看次数

完成Android中的父级和当前活动

我有3个活动.活动A导致活动B,活动B又可以返回活动A或开始活动C.但是,如果我在活动C中按回应用程序应该关闭.

总结一下:

  • 活动A开始活动B.
  • 返回活动B应该导致A
  • 活动B开始活动C.
  • 返回活动C应该关闭应用程序

我应该如何从活动B转到C?这段代码目前在最后一行给我一个NullPointerException:

Intent intent=new Intent(ActivityB.this, ActivityC.class);
startActivity(intent);
ActivityB.this.finish();
ActivityB.this.getParent().finish();
Run Code Online (Sandbox Code Playgroud)

如果我切换最后两行,我也会得到一个空指针.

android android-activity

49
推荐指数
4
解决办法
9万
查看次数

有用的Android系统资源

Android附带了许多系统资源(android.R),可用于节省您的时间并使您的应用程序更轻松.

例如,我最近发现Android提供了Yes(android.R.string.yes),No(android.R.string.no),Cancel(android.R.string.cancel)和Ok(android.R.string.ok)以及其他字符串的本地化字符串.

您建议使用哪些其他系统资源?或者有理由避免使用系统资源吗?

编辑:作为由Tomas指出,一些这方面的资源可能不会产生你所期望的结果(特别是android.R.string.yes/no返回OK/Cancel代替Yes/No,如报告在这里).为了获得更好的控制,您可以从Android源代码中复制系统资源.

optimization resources android

48
推荐指数
3
解决办法
3万
查看次数

重新排序UICollectionView的单元格

考虑UICollectionView流量布局和水平方向.默认情况下,单元格从上到下,从左到右排序.像这样:

1 4 7 10 13 16
2 5 8 11 14 17
3 6 9 12 15 18
Run Code Online (Sandbox Code Playgroud)

在我的例子中,集合视图被分页并且它被设计为使得特定数量的单元格适合每个页面.因此,更自然的顺序是:

1 2 3   10 11 12
4 5 6 - 13 14 15
7 8 9   16 17 18
Run Code Online (Sandbox Code Playgroud)

如果没有实现我自己的自定义布局,最简单的方法是什么?特别是,我不想放弃任何免费的功能UICollectionViewFlowLayout(例如插入/删除动画).

或者一般来说,如何f(n)在流布局上实现重新排序功能?例如,这可以适用于从右到左的排序.

到目前为止我的方法

我的第一种方法是子类化UICollectionViewFlowLayout和覆盖layoutAttributesForItemAtIndexPath::

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *reorderedIndexPath = [self reorderedIndexPathOfIndexPath:indexPath];
    UICollectionViewLayoutAttributes *layout = [super layoutAttributesForItemAtIndexPath:reorderedIndexPath];
    layout.indexPath = indexPath;
    return layout;
}
Run Code Online (Sandbox Code Playgroud)

哪里reorderedIndexPathOfIndexPath:f(n).通过调用super …

ios ios6 uicollectionview uicollectionviewlayout

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

如何在Objective-C中命名常量?

Objective-C中常量的命名约定是什么(或者最常用的命名方式)?

extern常量有不同的标准吗?

我见过的一些款式:

NSString* const kPreferenceFirstRun = @"FirstRun";

// Replace "XY" by a prefix representing your company, project or module
NSString* const XYPreferenceFirstRun = @"FirstRun"; 
Run Code Online (Sandbox Code Playgroud)

const constants objective-c naming-conventions extern

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

覆盖子类中的init

在Objective-C中,是否有必要覆盖子类的所有继承构造函数以添加自定义初始化逻辑?

例如,对于UIView具有自定义初始化逻辑的子类,以下内容是否正确?

@implementation CustomUIView

- (id)init {
    self = [super init];
    if (self) {
        [self initHelper];
    }
    return self;
}

- (id)initWithFrame:(CGRect)theFrame {
    self = [super initWithFrame:theFrame];
    if (self) {
        [self initHelper];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super initWithCoder:decoder];
    if (self) {
        [self initHelper];
    }
    return self;
}

- (void) initHelper {
    // Custom initialization
}

@end
Run Code Online (Sandbox Code Playgroud)

inheritance initialization objective-c init uiview

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

在iOS中绘制部分图像的最有效方法

给定a UIImage和a CGRect,绘制与CGRect(不缩放)对应的图像部分的最有效方式(在内存和时间上)是什么?

作为参考,这是我目前的做法:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect frameRect = CGRectMake(frameOrigin.x + rect.origin.x, frameOrigin.y + rect.origin.y, rect.size.width, rect.size.height);    
    CGImageRef imageRef = CGImageCreateWithImageInRect(image_.CGImage, frameRect);
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, rect, imageRef);
    CGImageRelease(imageRef);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,对于中等大小的图像和高setNeedsDisplay频率,这似乎非常慢.使用UIImageView框架并clipToBounds产生更好的结果(灵活性较低).

iphone uiimage drawrect quartz-2d ios

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

查找ARC保留对象的位置

我有一个对象被保留超过必要(很可能是由于一个属性strong而不是weak).大代码库,所以很难找到哪里.

使用ARC时,如何找到保留此对象的所有行?

如果我没有使用ARC,我想我可以简单地覆盖retain并检查它的调用位置.我可以用ARC做类似的事吗?

macos xcode ios automatic-ref-counting

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

在哪里突出显示UICollectionViewCell:委托还是单元格?

根据Collection View编程指南,应该处理单元格高光的视觉状态UICollectionViewDelegate.像这样:

- (void)collectionView:(PSUICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell highlight];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell unhighlight];
}
Run Code Online (Sandbox Code Playgroud)

我不喜欢这种方法的是它为委托添加了非常特定于单元格的逻辑.事实上,UICollectionViewCell通过highlighted财产独立管理其突出显示的状态.

那么,压倒一切setHighlighted:不是一个更清洁的解决方案吗?

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    if (highlighted) {
        [self highlight];
    } else {
        [self unhighlight];
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法有没有缺点而不是委托方法?

ios ios6 uicollectionview uicollectionviewcell

34
推荐指数
5
解决办法
3万
查看次数