How to write into local storage under Xcode

I made a game app wirh Codea which saves a tentative results file in Documens. The app will read the file as a current record to resume the game. But when I export the code into Xcode and built in a target iPad, it works well except for the tentetive results is missing. The record the app reads is the original one and never been updated. Also, I always get an error message from Xcode for ‘saveImage(“Project:Icon”,myImage)’ which is useless for Xcode though.
Is there any way to save data in the target machine?

@Makoto, this is probably because there isn’t really a Projects folder, I think that only works in Codea. I’ll look into it tho, because I’m pretty sure it should work in xCode

Thank you for you prompt reply.
The target file is placed in Documents folder in Codea and is rewrtten to reflect the progress of the game using “saveText()” command. After exporting it into xCode, the file is found in the folder " projectname¥Resorces¥Assets¥Documents.assets" in xCode.
This file is supposed to be overwitten during the game, however, the time stamp of the file remain the original one after I run the game with the simulator of Mac. And the contents of the file looks as the same as the original one since the restarted game begins with the original situation.

Also, please look into another issue;
The command “saveImage(“Priject:Icon”, myImage”) gets error from xCode compiler. I can find the original file “Icon.png” at ¥Assets¥Project.assets in xCode.

It’s probably because there is an extra quotation mark, that’s all I can think of right now. Because sometimes, xCode automatically adds a quotation mark,even when it is not necessary. Just make sure of the syntax, because xCode tends to screw things up with Codea files. Sorry, but that’s all I can tell you.

Then you for your advice. I just want to cralify your suggestion.
The file is a record of sequence of numbers with “#” as the separater. (I tried the separater as “,” , but the result was the same.)
When I open the file by xCode editor, there are no quotation marks and the record was sa was. Where shoul I look into to check the “syntax” other than the data itself?
Or is there any other better way to record progress on local memory?

@Makoto to be honest, I prefer not to use the readText() function because it isn’t as good as the readLocalData() function. I advise that you make a table, and modify the table, then when you want to save it, do

table=json.encode(table)
saveLocalData("key",table)

then when you want to retrieve the data, you put at the beginning:

local dataRetriever = readLocalData("key","{}")
table=json.decode(dataRetriever)

Then at that point, you will have a table, with any data you want to save. Sorry for not writing this code more formally, I’m typing this on a computer. Hope that helps!

I really appreciate you support. I tried your suggestion, and it worked.
But I am curiose where the LocalData is stored and is there any way to access or see the file? Related question is how the GlobalData works? Can it be accessed from other apps such as Memo or GoodReader?

@Makoto Great! I’m glad it worked for you! And the main difference between globalData and localData is that globalData can be saved throughout all your codea projects, its not global to the extent that it could be accessed to external applications.

Have you looked at this thread? Particularly the suite of tests that @zapaper wrote?

https://codea.io/talk/discussion/6771/problem-with-saveimage

Hi CamelCoder,
Thank you for your clarification that globalData is only accessible among Codea apps. Then is there any way to access colud servers, such as Dropbox?

@Makoto There is no way from Codea to access cloud servers, the only way to do it is to write a lot of Objective-C code and do it when you export it to xCode. Sorry

OK. Thanks.

Hi CamleCoder, again.

I investigated behavior of the GlobalData since I need to share data among several projects. But their are some remaining issues.

First, under Codea, I can save data and retrieve it from other projects.
Only the issue is how to list the assets in the GlobalData.
How can I use listGlobalData()?

With commands below, I can get one name of asset as str.
tbl=listGlobalData()
str=table.unpack(tbl)
And I can get the table saved as str.
data=readGlobalData(str,{})
m=json.decode(data)
When I save another table in the GlobalData as str2, I can retrieve the asset by its name, however listGlobalData() does not return its asset name but the name obtained above. Of course, I can read the asset named str as well as str2.

Also, is there any way to erase or clear GlobalData, similar to clearLocalData()?

Then I exported the code to Xcode and tested how it works.
Both saveGloablData() and readGlobalData() work for several assets within the project. But again the list GlobalData() retrns only one name.

When I switch to another project, listGlobalData() does find one asset which can be read with readGlobalData(). However, I can not retrieve other assets using their asset name which I have saved in the above process. They are disappeared, but here are they gone?

Please teach me the correct way to retrieve assets saved by other projects.
Thanks.

  • Makoto

@Makoto First of all, the issue regarding listGlobalData. You said

str=table.unpack(tbl)

the table.unpack() function returns all the element of the table, so str will of course only grab the first one. I would rather say:

function getAllGlobalData()
local gDataTable=listGlobalData()
local dataHolder={}
for i,v in ipairs(gDataTable) do
local data=readGlobalData(v)
table.insert(dataHolder,data)
end
return dataHolder
end

This will return all the global data in the form of a table. As for the clearGlobalData(), that is not a function, but a loophole is to say…

function clearGlobalData()
for k,v in ipairs(listGlobalData()) do
saveGlobalData(v,nil)
end
end

Again, listGlobalData() returns all the keys, but if you do table.unpack() and assign it to only one variable, it will seem as if it is only returning the first key.
Good luck!