小编Ang*_*han的帖子

当原始类不可用时,如何解码对象?

我有一个iOS 7应用程序,它将自定义对象作为文件保存到应用程序的iCloud Docs文件夹中.为此,我使用了NSCoding协议.

@interface Person : NSObject <NSCoding>

    @property (copy, nonatomic) NSString *name
    @property (copy, nonatomic) NSString *lastName

@end
Run Code Online (Sandbox Code Playgroud)

对象序列化在iOS 7版本的应用程序中完美运行:

  1. initWithCoderencodeWithCoder

  2. [NSKeyedArchiver archivedDataWithRootObject:person]

  3. person = NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)theData]

但我需要将此应用程序移至iOS 8,此类将以swift编码,并为此iOS应用程序的新版本重新命名.

class PersonOldVersion: NSObject, NSCoding {
    var name = ""
    var lastName = ""
}
Run Code Online (Sandbox Code Playgroud)

当我尝试取消归档该对象时,我收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (Person)'
Run Code Online (Sandbox Code Playgroud)

我已经尝试将swift类'PersonOldVersion'重命名为原始类名('Person'),但仍然失败.

如何解码原始类不可用的对象?

nscoding ios nskeyedunarchiver swift

38
推荐指数
4
解决办法
8586
查看次数

将边框添加到自定义CollectionView

如何在UICollectionView使用自定义的单元格中添加边框UICollectionViewFlowLayout?当我覆盖UICollectionView的流布局时,单元格边框被"删除".如何正确设置边框?谢谢!

ios uicollectionview uicollectionviewcell uicollectionviewlayout swift

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

UITextView上边距

我正在使用textview并注意到默认情况下iOS 7会留下一个上边距.请参阅以下图像在此输入图像描述

我阅读了最常用解决方案的不同帖子:

[textViewTest setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
Run Code Online (Sandbox Code Playgroud)

但这些插入只是特定设备,textview,字体大小等的自定义解决方案.因此,没有适用于任何解决方案的特定插件......即使最糟糕的是,我还必须以编程方式定义不同的插件以考虑所有iOS设备和方向.

好消息是,我发现每当textview成为第一个响应者并且键盘显示在屏幕上时,即使键盘消失,这个上边距也会消失.顺便说一句,我正在调整UIKeyboardDidShowNotification和UIKeyboardWillHideNotification上的contentInset.

  • 键盘确实显示时看到图像:

在此输入图像描述

  • 键盘消失时看图像:

在此输入图像描述

有没有办法模拟键盘显示和隐藏?因此内容插入消失了,如上所述.

我已经尝试使textview成为第一响应者然后重新签名,但是对于这种方法,用户必须看到整个键盘显示隐藏动画.

提前致谢!

我的代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    if(self.topMarginIsAlreadyResized == NO) {
        [self.myTextView becomeFirstResponder]; // Keyboard will show to eliminate top margin when view appears
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)handleKeyboardDidShow:(NSNotification *)notification {
    if(self.topMarginIsAlreadyResized == NO) {
        self.topMarginIsAlreadyResized …
Run Code Online (Sandbox Code Playgroud)

uitextview uikeyboard ios

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

用于自定义视图的 SwiftUI ViewModifier

有没有办法创建修改器来更新@State private var正在修改的视图中的a ?

我有一个自定义视图,它返回Text带有“动态”背景颜色的 a 或Circle带有“动态”前景色的 a。

struct ChildView: View {
    var theText = ""
    
    @State private var color = Color(.purple)
    
    var body: some View {
        HStack {
            if theText.isEmpty {          // If there's no theText, a Circle is created
                Circle()
                    .foregroundColor(color)
                    .frame(width: 100, height: 100)
            } else {                      // If theText is provided, a Text is created
                Text(theText)
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 25.0)
                                    .foregroundColor(color))
                    .foregroundColor(.white)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序的不同部分重复使用这个视图。如您所见,我需要指定的唯一参数是theText. 因此,创建此 ChildView …

swiftui viewmodifier swiftui-state

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

SwiftUI 转换有不同的实现吗?

我一直在测试基于SwiftUI 实验室的过渡如何工作,但我发现过渡不应该全部以相同的方式实现。基于上一篇文章:

请注意,自 XCode 11.2 起,过渡不再适用于隐式动画

事情是,我发现了一些奇怪的事情。当使用隐式动画或将动画与其关联时,某些过渡效果很好。那么,应该使用什么动画呢?这得看情况。什么?我不知道。我希望有人能帮我解释这一切。

在下面的测试中,我创建了 5 个视图,每个视图都与不同的转换相关联:.opacity.scale.move.slidecombined(.opacity.move)。以下是我的发现:

注意:我正在使用 Xcode 11.4 和模拟器!

使用隐式动画的过渡

只有.move.slide转换可以正常工作(删除和插入)。

在此输入图像描述

使用显式动画的过渡

删除和插入动画适用于所有过渡。

在此输入图像描述

将动画与每个转换关联

只有.scale.opacity转换可以正常工作(删除和插入)。

在此输入图像描述

从我的角度来看,没有实现转换的标准方法会使事情变得复杂,而且在组合转换时 ( .combined)。

我是否遗漏了一些关于实施的内容?这是我的代码:

struct TestAnimation: View {
    @State var show : Bool = true
    var colors : [Color] = [.orange, .yellow, .green, .blue, .pink]

    var body: some View {
        VStack(alignment: .leading) {
            Spacer()

            Color.purple …
Run Code Online (Sandbox Code Playgroud)

transition swiftui swiftui-animation

5
推荐指数
0
解决办法
1779
查看次数

swift NSDateFormatter无法正常工作

我正在尝试格式化时间如下:

let formatter = NSDateFormatter()
formatter.dateFormat = "hh_mm_ss"
let d = formatter.stringFromDate(NSDate())
println("formatted text is: \(d)")
Run Code Online (Sandbox Code Playgroud)

假设当前时间是2014年9月3日22:15:30

  1. 当我在操场上运行此脚本时,它会打印正确格式化的时间:22_15_30.
  2. 在AppDelegate上运行时,它不会打印格式化的时间: 22:15:30

我正在使用xcode 6 beta 5 ...我错过了什么吗?为什么stringFromDate没有返回格式正确的日期?

编辑:我正在使用xcode 6 beta 6.

谢谢!

nsdate nsdateformatter ios swift

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

takeUnretainedValue和takeRetainedValue有什么区别?

takeUnretainedValue和之间有什么区别takeRetainedValue?根据Apple的文档:

这两种方法都返回原始的,未包装的对象类型.您可以根据要调用的API是否返回未保留或保留的对象来选择要使用的方法.

但是,我如何知道非托管对象是未保留对象还是保留对象?例如,该方法ABAddressBookCreateWithOptions:AddressBook框架返回一个非托管对象ABAddressBook,其中一些教程认为这是一个保留的对象.使用时ALAsset,该方法thumbnail返回一个非托管对象CGImage,一些教程认为这是一个未保留的对象.

我将感激你的帮助.

memory-management retained-in-memory unsafe-unretained swift

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