首页 \ 问答 \ 将自定义对象保存到NSUserDefaults中[重复](Save custom objects into NSUserDefaults [duplicate])

将自定义对象保存到NSUserDefaults中[重复](Save custom objects into NSUserDefaults [duplicate])

这个问题在这里已经有了答案:

我有一个新闻ViewController和一个TeamViewControllerTeamViewController包含一个teamView的tableView,它在被选中时被添加到数组中。 我想将此数组添加到NSUserDefaults以便我可以从NewsController访问它们,其中包含需要teamObjects的url请求。 不过,我不断收到:

'尝试插入非属性列表对象(“”)

我打开其他建议,如果有更好的方法比存储在NSUserDefaults

didSelectRowAtIndexPath方法

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)


    let team = self.teamArray[indexPath.row] as Team
    var removed = false

    for (index, value) in enumerate(self.teamSelected) {
        if (value == team) {
            self.teamSelected.removeAtIndex(index)
            removed = true
        }
    }

    if (!removed) {
        self.teamSelected.append(team)
    }

    var userDefaults = NSUserDefaults.standardUserDefaults()
    userDefaults.setValue(self.teamSelected, forKey: "teams")
    userDefaults.synchronize()

    tableView.reloadData()
}

我的对象

class Team: NSObject{
    var id: Int!
    var name: NSString!
    var shortname: NSString!


    init(id: Int, name:NSString, shortname: NSString) {
        self.id = id
        self.name = name
        self.shortname = shortname

    }

}

This question already has an answer here:

I'm having a news ViewController and a TeamViewController. The TeamViewController contain a tableView of teamObjects which when selected is added into array. I want to add this array into NSUserDefaults so i can access them from the NewsController which contain a url request where the teamObjects is needed. However i keep getting:

'Attempt to insert non-property list object ( "" ) for key teams'

I'm open for other suggestions if there is better ways than storing it in NSUserDefaults

didSelectRowAtIndexPath method

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)


    let team = self.teamArray[indexPath.row] as Team
    var removed = false

    for (index, value) in enumerate(self.teamSelected) {
        if (value == team) {
            self.teamSelected.removeAtIndex(index)
            removed = true
        }
    }

    if (!removed) {
        self.teamSelected.append(team)
    }

    var userDefaults = NSUserDefaults.standardUserDefaults()
    userDefaults.setValue(self.teamSelected, forKey: "teams")
    userDefaults.synchronize()

    tableView.reloadData()
}

My object

class Team: NSObject{
    var id: Int!
    var name: NSString!
    var shortname: NSString!


    init(id: Int, name:NSString, shortname: NSString) {
        self.id = id
        self.name = name
        self.shortname = shortname

    }

}

原文:https://stackoverflow.com/questions/29986957
更新时间:2021-10-26 21:10

最满意答案

laravel 部署到万网虚拟主机上:
改变一下Laravel的文件目录结构,为应用选择低位的加密方式,就可以在万网云虚拟主机上成功运行。
拿laravel5.1来说:

在根目录下创建一个local文件夹,把网站根目录下除了public文件夹以外所有文件及文件夹复制到local文件夹中
然后把public文件夹下的所有文件复制到网站根目录,接着删除public文件夹,这样入口就变成根目录而不是public了
打开根目录下的index.php(之前/public/index.php)
将
require __DIR__.'/../bootstrap/autoload.php';
修改为
require __DIR__.'/local/bootstrap/autoload.php';
将
$app = require_once __DIR__.'/../bootstrap/app.php';
修改为
$app = require_once __DIR__.'/local/bootstrap/app.php';
打开/local/config/app.php(之前的/config/app.php)
将
'key' => env('APP_KEY', 'SomeRandomString'),
'cipher' => 'AES-256-CBC',
改为
'key' => env('APP_KEY', '1234567890qwerty'),
'cipher' => 'AES-128-CBC',
其中key使用长度为16的随机字符串,由于万网不支持'AES-256-CBC'所以用128位加密就可以了,128用16个随机字符串做key,256用32个。
上传到万网虚拟主机,然后再后台把PHP版本改成5.5就可以了,妥妥的就运行成功laravel了。

其他回答

laravel 部署到万网的虚拟主机上: 改变一下laravel的文件目录结构,为应用选择低位的加密方式,就可以在万网云虚拟主机上成功运行。 拿laravel5.1来说: 在根目录下创建一个local文件夹,把网站根目录下除了public文件夹以外所有文件及文件夹复制到local文件夹中 然后把public文件夹下的所有文件复制到网站根目录,接着删除public文件夹,这样入口就变成根目录而不是public了 打开根目录下的index.php(之前/public/index.php) 将 require __dir__.'/../bootstrap/autoload.php'; 修改为 require __dir__.'/local/bootstrap/autoload.php'; 将 $app = require_once __dir__.'/../bootstrap/app.php'; 修改为 $app = require_once __dir__.'/local/bootstrap/app.php'; 打开/local/config/app.php(之前的/config/app.php) 将 'key' => env('app_key', 'somerandomstring'), 'cipher' => 'aes-256-cbc', 改为 'key' => env('app_key', '1234567890qwerty'), 'cipher' => 'aes-128-cbc', 其中key使用长度为16的随机字符串,由于万网不支持'aes-256-cbc'所以用128位加密就可以了,128用16个随机字符串做key,256用32个。 上传到万网虚拟主机,然后再后台把php版本改成5.5就可以了,妥妥的就运行成功laravel了。

相关问答

更多
  • 我的部署方式: 本地和线上都是nginx,先配好rewrite规则(就是把.htaccess里面的规则原样翻译到nginx.conf里面去) 服务器上,我是直接导SQL, 毕竟我还不习惯用php命令去创建数据库,当然了,这个见仁见智了,laravel的迁移功能是很强大的,在同步本地...
  • laravel 部署到万网的虚拟主机上: 改变一下Laravel的文件目录结构,为应用选择低位的加密方式,就可以在万网云虚拟主机上成功运行。 拿laravel5.1来说: 在根目录下创建一个local文件夹,把网站根目录下除了public文件夹以外所有文件及文件夹复制到local文件夹中 然后把public文件夹下的所有文件复制到网站根目录,接着删除public文件夹,这样入口就变成根目录而不是public了 打开根目录下的index.php(之前/public/index.php) 将 require _ ...
  • 由于您将集合传递给资源,因此无法直接访问Modal的属性。 相反,对于资源实例,您将获得一个Collection,让您可以访问收集方法 。 所以你可以改变你的资源toArray() public function toArray($request) { return $this->pluck('word_name')->keyBy(function ($item) { return 'word'; }); } Since you are passing a collec ...
  • 因此,我们可以使用Doctrine将Data Mapper模式实现到Laravel中; Packagist: https ://packagist.org/packages/atrauzzi/laravel-doctrine Laravel Doc: http ://bundles.laravel.com/bundle/doctrine So, we can implement the Data Mapper pattern into Laravel using Doctrine; Packagist: h ...
  • 我的典型清单: 将文档根目录修改为/ public文件夹 使/storage和bootstrap/cache文件夹可写。 设置数据库 修改.env文件以适应实时环境 运行php artisan migrate 这应该让你启动并运行,或者至少让你的错误足够详细以解决问题 My typical checklist: Modify the document root to the /public folder Make the /storage and bootstrap/cache folders writea ...
  • 如果是,那么是共享主机吗?那么: 将公共(L5)的内容直接放在public_html上(注意不要意外覆盖.htaccess文件),然后修改你的index.php和你的bootstrap.php,它会工作得很好 Is it a shared hosting you're using if yes then : Put the content of public (L5) directly on public_html (be aware of don't overwrite the .htaccess fil ...
  • 我再看看我的文件夹结构,我注意到作曲家没有正确运行并且丢失了文件。 我已重新安装它,它似乎正在工作。 I had another look at my folder structure and I noticed that composer had not run correctly and there were missing files. I've reinstalled it and it appears to be working.
  • 我做了那个样板。 该命令特定于下载Laravel Elixir软件包的依赖项( http://laravel.com/docs/5.0/elixir )但是除此之外还有许多用途。 Elixir文档很好地解释了它。 I made that boilerplate. That command is specific to download the dependencies for the Laravel Elixir package (http://laravel.com/docs/5.0/elixir) Bu ...
  • $ _ClientId和$ _ClientSecret对应。 您需要创建具有正确ID和密码的应用程序 ,这通过https://developer.paypal.com完成。创建应用程序后,您将在应用程序详细信息中找到这些信息。 编辑 使用相同的库和Laravel查看本教程视频 ,大约3:20的人正在显示您需要做什么。 $_ClientId and $_ClientSecret correspond to. You need to create your application to have right i ...
  • 在共享主机上你不能运行artisan命令,你不能享受共享主机上的所有laravel功能,你需要vps或专用服务器,在共享主机上你需要做很多手工工作,比如旧的php网站上传和数据库更新,如果您想在共享主机上上载laravel项目,请按照以下步骤进行操作。 在托管根目录上创建一个目录名框架,并从本地目录上除了公共目录之外的所有内容。 从主机上www目录中的公共目录上传所有内容。 现在修改你的index.php文件 转到index.php并编辑第22行 #From this require __DIR__.'/. ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)