我正在尝试读取我在NSUserdefaults中保存的数组。 关于如何使用“objectforkey”或“arrayforkey”的大多数示例都抛出了“AnyObject”,它们在Swift 2.2中给出了错误。
此:
https://apis-explorer.appspot.com/apis-explorer/?base=https://ig-project-1280.appspot.com/_ah/api#p/userendpoint/v1/userendpoint.insertUser?_h=4&resource=%257B%250A++%2522password%2522%253A+%2522welcome1%2522%252C%250A++%2522userName%2522%253A+%2522vik.ceo%2540gmail.com%2522%250A%257D&
and did the insert operation. The error come up is:
com.google.api.server.spi.SystemService invokeServiceMethod: exception occurred while calling backend method
java.lang.NullPointerException
at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:318)
at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:256)
at photography.viks.app.ig.entity.UserEndpoint.containsUser(UserEndpoint.java:152)
at photography.viks.app.ig.entity.UserEndpoint.insertUser(UserEndpoint.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:44)
at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:363)
at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:113)
at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:256)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
at java.lang.Thread.run(Thread.java:745)
说不能使用AnyObjet
此:
if let array NSUserDefaults.standardUserDefaults().objectForKey("mykey")
崩溃应用程序说“超出范围”
此:
let array NSUserDefaults.standardUserDefaults().objectForKey("mykey") as? [String: String] ?? [String: String]()
也崩溃了。
有人可以帮我解决如何读取像objectforkey这样的AnyObject的arrayforkey
答案 0 :(得分:0)
保存数组:
let array:[String] = ["one","two"]
NSUserDefaults.standardUserDefaults().setObject(array, forKey: "array")
NSUserDefaults.standardUserDefaults().synchronize()
检索数组:
if let retrievedArray:[String] = NSUserDefaults.standardUserDefaults().objectForKey("array") as? [String]{
print("\(retrievedArray)")
}