小编Leg*_*ess的帖子

使用相同的超类合并Swift数组

我正在尝试找到合并Swift数组的最佳方法,它们的类型不同,但它们具有相同的超类.我已经阅读了所有教程,所以我知道我可以使用:

var array = ["Some", "Array", "of", "Strings"]
array += ["Another string", "and one more"]
array.append(["Or", "This"])
Run Code Online (Sandbox Code Playgroud)

在这种情况下,array变量推断出类型[String].我遇到的问题与类的下一个假设情况有关(在这种情况下属性无关紧要,只是为了在类之间产生差异):

class A {
    var property1 : String?
}

class B : A {
    var property2: String?
}

class C : A {
    var property3: String?
}
Run Code Online (Sandbox Code Playgroud)

所以我创建了一个类AB对象的数组:

var array = [ A(), B(), A(), B() ]
Run Code Online (Sandbox Code Playgroud)

此数组现在应该是类型[A],因为这是两个类AB类的推断类型.

现在我想将对象追加到这个类型的数组C.

var anotherArray = [ …

macos ios swift

6
推荐指数
1
解决办法
1455
查看次数

UISwitch - 检测动画结束

我有一个UISwitch在里面UITableViewCell.我为切换分配了目标操作:

[switch addTarget:self action:@selector(changeSwitchColor:) forControlEvents:UIControlEventValueChanged];

- (void)changeSwitchColor:(id)sender
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

问题是changeSwitchColor:在动画完成之前调用,但我想检测动画何时完成,所以我可以在thumbTintColor不破坏动画的情况下设置属性.

我尝试使用UIView setAnimationDidStopSelector:方法检测动画:

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

但是在UISwitch完成动画时不会调用此方法(我甚至不确定动画是如何在内部制作的).

我怎么能检测到完成状态UISwitch

谢谢!

uiswitch ios ios8

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

使用位码更改 LC_ID_DYLIB 重新编译

我正在从启用位码的 iOS 源代码构建一个动态框架(使用cmakexcodebuild)。我使用lipoinstall_name_tool制作一个胖二进制文件并 update LC_ID_DYLIB,以便正确加载二进制文件。当我归档应用程序时,框架已正确签名并与应用程序一起打包。这是以下的输出file

MyFramework: Mach-O universal binary with 3 architectures: [arm_v7: Mach-O dynamically linked shared library arm_v7] [arm_v7s] [arm64]
MyFramework (for architecture armv7):   Mach-O dynamically linked shared library arm_v7
MyFramework (for architecture armv7s):  Mach-O dynamically linked shared library arm_v7s
MyFramework (for architecture arm64):   Mach-O 64-bit dynamically linked shared library arm64
Run Code Online (Sandbox Code Playgroud)

查看otool -l输出LC_ID_DYLIB显示:

Load command 4
          cmd LC_ID_DYLIB
      cmdsize 64
         name @rpath/MyFramework.framework/MyFramework (offset 24) …
Run Code Online (Sandbox Code Playgroud)

frameworks mach-o dylib otool ios

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

Swift 4 Objective-C 运行时和转换为 NSObjectProtocol

在 Swift 3 中,我有一段代码调用 Objective-C 运行时来检查某个类是否存在。

guard let managerClass = NSClassFromString("ASIdentifierManager") as? NSObjectProtocol else {
    return nil
}
Run Code Online (Sandbox Code Playgroud)

这将返回AnyClass实例并且它可以转换为NSObjectProtocol- 所以我可以调用像respondsperform使用选择器这样的方法。

在 Swift 4 中,这在运行时仍然有效,但会发出警告:

来自“AnyClass?”的演员表 (又名“可选”)到不相关的类型“NSObjectProtocol”总是失败

奇怪的是它仍然有效,但我担心某个 Swift 版本会杀死代码。

摆脱此警告的正确方法是什么?我应该将对象转换为什么,以便能够对其动态执行选择器?

objective-c-runtime foundation swift swift4

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

当旋转时,UIView的drawRect没有被调用

我正在尝试使用先进的Objective-C方法.我想要实现的是将特定的绘图代码附加到现有的UIView.

我开始很容易,我继续drawRect在一个类别中声明我自己的方法:

@interface UIView (Swizzled)

- (void)my_drawRect:(CGRect)rect;

@end
Run Code Online (Sandbox Code Playgroud)

然后我在实施类别中调整了drawRect方法UIView:

+ (void)load
{
    [self swizzleInstanceMethod:@selector(drawRect:) withMethod:@selector(my_drawRect:) inClass:[UIView class]];
}

- (void)my_drawRect:(CGRect)rect
{
    NSLog (@"Test.");
}
Run Code Online (Sandbox Code Playgroud)

GitHub上提供了这种方法的实现,并且在所有其他情况下都可以使用这种方法.

所以根据逻辑,每次drawRect调用时,都应该打印出"Test".但它不起作用,我调用的方法从未被调用过.我接着发现了我在这里做错了什么,但是我看的越多,我就越相信问题就在其他地方.

如果drawRect甚至没有调用该方法怎么办?我接着试着强迫它被召唤:

view.contentMode = UIViewContentModeRedraw;
[view setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)

也不起作用.

所以我的问题是:

在这种情况下如何强制UIView调用drawRect甚至是我的调配实现?

谢谢!

core-graphics objective-c drawrect swizzling ios

3
推荐指数
1
解决办法
438
查看次数

如何检查UIFont是否是系统字体?

我有以下代码创建多个字体.

UIFont* systemFont1 = [UIFont systemFontOfSize:12.0];
UIFont* systemFont2 = [UIFont boldSystemFontOfSize:12.0];
UIFont* systemFont3 = [UIFont italicSystemFontOfSize:12.0];

UIFont* customFont1 = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0];
UIFont* customFont2 = [UIFont fontWithName:@"HelveticaNeue-Regular" size:12.0];
UIFont* customFont3 = [UIFont fontWithName:@"HelveticaNeue-Thin" size:12.0];

UIFont* customFont4 = [UIFont fontWithName:@"MyriadPro" size:12.0];
UIFont* customFont5 = [UIFont fontWithName:@"MyriadPro-Italic" size:12.0];
UIFont* customFont6 = [UIFont fontWithName:@"MyriadPro-Condensed" size:12.0];
Run Code Online (Sandbox Code Playgroud)

我想知道哪个UIFont是系统.我几乎需要将返回的方法BOOL YES变量:systemFont1,systemFont2,systemFont3和NOcustomFont4,customFont5,customFont6.

由于Helvetica Neue是iOS7上的系统字体,因此无论是应该返回NO还是YES在这些情况下都会引起争议,但对于我的问题,无论哪种方式都可以.

所以我的问题是:

如何验证UIFont实例是否是由系统字体方法之一创建的?

谢谢您的帮助!

iphone fonts uikit uifont ios

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

Swift 中 URLSession 的子类化工厂方法

我正在尝试URLSession在 Swift 中创建一个子类(原因无关紧要,但与测试有关)。我需要它与 adelegate和特定的 一起使用URLSessionConfiguration,这是 上的只读属性URLSessionURLSession使用委托进行初始化的通常方法是使用以下代码完成的,它可以完美地工作:

let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil)
Run Code Online (Sandbox Code Playgroud)

现在让我们创建一个子类:

class MyURLSession : URLSession {}

let session = MyURLSession(configuration: 
URLSessionConfiguration.default, delegate: nil, delegateQueue: nil) // Compile error
Run Code Online (Sandbox Code Playgroud)

初始化程序触发下一个编译错误:

error: argument passed to call that takes no arguments

根据 Swift Language Guide rule 1 for Automatic Initializer Inheritance

If your subclass doesn’t define any designated initializers, it 
automatically inherits all of its superclass designated …
Run Code Online (Sandbox Code Playgroud)

inheritance initializer factory-method swift urlsession

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

如何在xcode中的同一页面内制作两个collectionView?

我想UICollectionView在同一页面上有两个.两个UICollectionView将根据需要显示不同的数据.怎么做?先感谢您.

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifierHall2 = @"hall2";

    /* Uncomment this block to use subclass-based cells */
    timeCell *cell = (timeCell *)[myCollectionViewHall2 dequeueReusableCellWithReuseIdentifier:cellIdentifierHall2 forIndexPath:indexPath];

    [cell.timeButton setTitle:[[allSimilarMutableArray valueForKey:@"showTime"] objectAtIndex:indexPath.item] forState:UIControlStateNormal];


    return cell;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifierHall3 = @"hall3";

    /* Uncomment this block to use subclass-based cells */
    timeCell *cell = (timeCell *)[myCollectionViewHall3 dequeueReusableCellWithReuseIdentifier:cellIdentifierHall3 forIndexPath:indexPath];

    [cell.timeButton setTitle:[[allSimilarMutableArray valueForKey:@"showTime"] objectAtIndex:indexPath.item] forState:UIControlStateNormal];


    return cell;

}
Run Code Online (Sandbox Code Playgroud)

collectionview ios uicollectionviewcell

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