从核心数据中的实体获取nil值

时间:2018-02-16 09:16:53

标签: ios swift core-data nsmanagedobject

我正在关注raywenderlich的核心数据教程:Tutorial

我面临的问题是在这段代码中:

sub inputbox_verification()

 text=inputbox("type the text")

 if StrPtr(text)=0 then
 'if it entenrs here then the user pressed "cancel"
 endif

 if text=""
 'if enters here the user left in blank
 end if

 if text<>""
 'if enters here the user entered some text
 end if

 end sub 

实体值返回let entity = NSEntityDescription.entity(forEntityName: "Person", in: managedContext)! let person = NSManagedObject(entity: entity,insertInto: managedContext) ,应用程序崩溃。有没有我错过的东西,因为我尝试了3-4次,但仍然是同样的问题

1 个答案:

答案 0 :(得分:-1)

如果你说你说的是正确的(在提到的行上崩溃),那么数据模型中就不存在Person实体。

请验证模特中是否存在此人:

enter image description here

在链接教程之后,我怀疑你正在调用save方法,如下所示:

@KeycloakConfiguration
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter
{
    private final KeycloakClientRequestFactory keycloakClientRequestFactory;

    public SecurityConfig(KeycloakClientRequestFactory keycloakClientRequestFactory)
    {
        this.keycloakClientRequestFactory = keycloakClientRequestFactory;
    }

    /**
   * define the actual constraints of the app.
   * @param http
   * @throws Exception
   */
  @Override
  protected void configure(HttpSecurity http) throws Exception
  {
      super.configure(http);
      http
      .cors()
      .and()
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
      .sessionAuthenticationStrategy(sessionAuthenticationStrategy())
      .and()
      .authorizeRequests()
          .antMatchers("/*").hasRole("user")
          .anyRequest().permitAll();
  }

    /**
     * define the session auth strategy so that no session is created
     * 
     * @return concrete implementation of session authentication strategy
     */
    @Bean
    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy()
    {
        return new NullAuthenticatedSessionStrategy();
    }

    /**
     * registers the Keycloakauthenticationprovider in spring context and sets its
     * mapping strategy for roles/authorities (mapping to spring seccurities'
     * default ROLE_... for authorities ).
     * 
     * @param auth
     *          SecurityBuilder to build authentications and add details like
     *          authproviders etc.
     * @throws Exception
     */
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
    {
        KeycloakAuthenticationProvider keyCloakAuthProvider = keycloakAuthenticationProvider();
        keyCloakAuthProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());

        auth.authenticationProvider(keyCloakAuthProvider);
    }

    /**
     * Sets keycloaks config resolver to use springs application.properties
     * instead of keycloak.json (which is standard)
     * 
     * @return
     */
    @Bean
    public KeycloakConfigResolver KeyCloakConfigResolver()
    {
        return new KeycloakSpringBootConfigResolver();
    }

    /**
     * Spring Boot attempts to eagerly register filter beans with the web
     * application context. Therefore, when running the Keycloak Spring Security
     * adapter in a Spring Boot environment, it may be necessary to add two
     * FilterRegistrationBeans to your security configuration to prevent the
     * Keycloak filters from being registered twice.
     * 
     * @param filter
     * @return
     */
    @Bean
    public FilterRegistrationBean keycloakAuthenticationProcessingFilterRegistrationBean(
            KeycloakAuthenticationProcessingFilter filter)
    {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
        registrationBean.setEnabled(false);
        return registrationBean;
    }

    /**
     * Spring Boot attempts to eagerly register filter beans with the web
     * application context. Therefore, when running the Keycloak Spring Security
     * adapter in a Spring Boot environment, it may be necessary to add two
     * FilterRegistrationBeans to your security configuration to prevent the
     * Keycloak filters from being registered twice.
     * 
     * @param filter
     * @return
     */
    @Bean
    public FilterRegistrationBean keycloakPreAuthActionsFilterRegistrationBean(KeycloakPreAuthActionsFilter filter)
    {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
        registrationBean.setEnabled(false);
        return registrationBean;
    }
}

这表明您还必须检查func save(name: String) { guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return } // 1 let managedContext = appDelegate.persistentContainer.viewContext // 2 let entity = NSEntityDescription.entity(forEntityName: "Person", in: managedContext)! let person = NSManagedObject(entity: entity, insertInto: managedContext) // 3 person.setValue(name, forKeyPath: "name") // 4 do { try managedContext.save() people.append(person) } catch let error as NSError { print("Could not save. \(error), \(error.userInfo)") } } 实体上的name属性:

enter image description here

我希望这会对你有所帮助。