Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将NSAttributedString保存到CoreData_Ios_Swift_Core Data_Uitextview_Nsattributedstring - Fatal编程技术网

Ios 如何将NSAttributedString保存到CoreData

Ios 如何将NSAttributedString保存到CoreData,ios,swift,core-data,uitextview,nsattributedstring,Ios,Swift,Core Data,Uitextview,Nsattributedstring,我想保存UItextview文本的attributedString。 我试图转换为字符串并保存。但是当我返回设置文本的attributedString时,它不起作用 以下是我转换为字符串的方式: var a = String(stringInterpolationSegment: text.attributedText) Data.setValue(a, forKey: "font") Data.managedObjectContext?.save(nil) 以下是我如何返回设

我想保存UItextview文本的attributedString。 我试图转换为字符串并保存。但是当我返回设置文本的attributedString时,它不起作用

以下是我转换为字符串的方式:

var a = String(stringInterpolationSegment: text.attributedText)
    Data.setValue(a, forKey: "font")
    Data.managedObjectContext?.save(nil)
以下是我如何返回设置:

text.attributedText = NSAttributedString(string: size)
但是我的文本视图只显示了AttributedSting

{
NSFont = "<UICTFont: 0x7fa9fa82aec0> font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}
{
NSFont=“font-family:\“Helvetica Neue\”字体重量:正常;字体样式:正常;字体大小:18.00pt”;
NSParagraphStyle=“对齐0、行距0、段落间距0、段落间距0、段落间距0、首缩进0、尾缩进0、第一行首缩进0、线宽0/0、线宽倍数0、换行模式0、制表符(\n 28L、\n 56L、\n 84L、\n 112L、\n 140L、\n 168L、\n 196L、\n 224L、\n 252L、\n 280L、\n 308L、\n 336L\n),DefaultTabInterval 0,Blocks(null),Lists(null),BaseWritingDirection-1,断字因子0,拉紧因子0,HeaderLevel 0”;
}
很抱歉,我不能发布图片。我用雨燕来做这件事


我的问题:我该怎么做?或者有其他方法保存attributedText吗?

是的,您可以通过简单的解决方法添加attributedText

添加类型为
可转换的属性。创建.h文件后,将该属性的类型更改为
nsattributestring
。在该属性中,可以轻松保存属性化字符串


希望这有帮助:)

该属性在数据模型中应如下所示

应修改头文件以匹配以下内容:

@property (nonatomic, retain) NSAttributedString * attributedText;
就这样。您应该能够像其他属性一样持久化属性字符串

假设您的实体是Event,并且您有一个Event类型的对象事件,您可以将其作为Event.AttributeText访问。以下是一些示例Swift代码:

event.attributedText = NSAttributedString(string: "Hello World")
let attributedString = event.attributedText

让我们知道您是否更喜欢母语的答案。

在具有自动代码生成功能的Xcode 10中,这比其他建议简单得多

  • 打开数据模型并选择属性的名称
  • 打开数据模型检查器(命令+选项+3)
  • 将属性类型设置为可转换
  • 自定义类设置为
    nsattributestring
  • 就这样,现在您可以按预期将数据保存在Swift代码中,而无需修改生成的类或强制转换,例如:


    detailItem.content=textView.attributedText

    可能重复@i_am_jorf这个问题我以前读过,它是object-c写的。我不知道怎么做是的,请参考i__jorf链接中pob21的答案。这不是公认的答案,但这是正确的做法。我在我的应用程序中也这样做(使用Swift)。@JoeSmith谢谢你的建议!我很抱歉问一个类似的问题。我只是一个快速入门的人。如果你不介意的话,你能分享你的代码吗?谢谢你的建议!我不知道你说的头文件是怎么写的。我是斯威夫特·贝格纳。希望你能慷慨地帮助我。:-)