我在同一个网站上读到了如何插入和UILabel(子类UILabel并覆盖所需的方法).在将它添加到我的应用程序之前,我决定在一个独立的测试应用程序中测试它.代码如下所示.
这是MyUILabel.h
#import <UIKit/UIKit.h>
@interface MyUILabel : UILabel
@end
Run Code Online (Sandbox Code Playgroud)
这是MyUILabel.m
#import "MyUILabel.h"
#import <QuartzCore/QuartzCore.h>
@implementation MyUILabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// for border and rounding
-(void) drawRect:(CGRect)rect
{
self.layer.cornerRadius = 4.0;
self.layer.borderWidth = 2;
[super drawRect:rect];
}
// for inset
-(void) drawTextInRect:(CGRect)rect
{
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect: UIEdgeInsetsInsetRect(rect, insets)];
}
Run Code Online (Sandbox Code Playgroud)
这是我的ViewController.h
#import <UIKit/UIKit.h>
#import "MyUILabel.h"
@interface ViewController : UIViewController
{
MyUILabel *myDisplay; …Run Code Online (Sandbox Code Playgroud) 我有一个简单的枚举,我想迭代.为此,我采用了Sequence和IteratorProtocol,如下面的代码所示.顺便说一句,这可以复制/粘贴到Xcode 8中的Playground.
import UIKit
enum Sections: Int {
case Section0 = 0
case Section1
case Section2
}
extension Sections : Sequence {
func makeIterator() -> SectionsGenerator {
return SectionsGenerator()
}
struct SectionsGenerator: IteratorProtocol {
var currentSection = 0
mutating func next() -> Sections? {
guard let item = Sections(rawValue:currentSection) else {
return nil
}
currentSection += 1
return item
}
}
}
for section in Sections {
print(section)
}
Run Code Online (Sandbox Code Playgroud)
但是for-in循环生成错误消息"Type'Sections.Type'不符合协议'Sequence'".协议一致性在我的扩展中; 那么,这段代码有什么问题?
我知道还有其他方法可以做到这一点,但我想了解这种方法有什么问题.
谢谢.
我有一个协议,我用于几个枚举,其中包括Swift 4.2的CaseIterable
public protocol CycleValue: CaseIterable {
/// Computed property that returns the next value of the property.
var nextValue:Self { get }
}
Run Code Online (Sandbox Code Playgroud)
我的CycleValue用例之一是使用Theme属性:
@objc public enum AppThemeAttributes: CycleValue {
case classic, darkMode // etc.
public var nextValue: AppThemeAttributes {
guard self != AppThemeAttributes.allCases.last else {
return AppThemeAttributes.allCases.first!
}
return AppThemeAttributes(rawValue: self.rawValue + 1)!
}
}
Run Code Online (Sandbox Code Playgroud)
我有其他用例; 例如,按钮类型.CaseIterable使nextValue的实现变得容易,但对于所有类型的CycleValue都是一样的.
我想实现CycleValue的扩展,它为nextValue属性提供默认实现,并避免重复代码(即:DRY!).
我一直在努力使用PAT(协议相关类型).似乎无法使语法正确.
它应该是可能的,对吗?如何为nextValue提供默认实现以避免重复ode?
在插入和/或删除一些单元格后,我必须对自定义UICollectionView进行一些更新。但是我想在动画(通过插入和/或删除触发)完成后进行更新。
如何知道UICollectionView何时插入/删除动画?
谢谢!
PS:在Swift中。
我看到过关于能够通过 SPM 使用混合语言项目的褒贬不一的评论。我有一个包含 Objective-C 代码的框架。我在混合语言项目(Objective-C 和 Swift)中使用该框架;具体来说,我需要在 Objective-C 代码中使用它。我已将框架转换为 Swift 包。该包编译良好。但我无法让 Objective-C 代码识别我的 Swift 包。旧框架一切正常。我已将新的 Swift 包添加到项目中。
当我使用“@import CalculatorModel;”时 我收到“未找到模块‘CalculatorModel’”。
我正在尝试将 header 导入到 Objective-C 代码中,如下所示:
#import <CalculatorModel/CalculatorModel.h>
Run Code Online (Sandbox Code Playgroud)
但是,当然,找不到标头。
这是我的 package.swift。
let package = Package(
name: "CalculatorModel",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "CalculatorModel",
targets: ["CalculatorModel"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: …Run Code Online (Sandbox Code Playgroud)