Objective c Basico, setName error EXEC_BAD_ACESS

Asked

Viewed 72 times

1

Good people when entering the setName of my class happens EXEC_BAD_ACESS error below goes the code:

Registration Class

//
//  Registro.h
//  Agenda
//
//  Created by Renan Vicente de Farias on 01/07/14.
//  Copyright (c) 2014 Renan Vicente de Farias. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Registro : NSObject

- (id) initWithNomeCelular:(NSString *)novoNome andCelular:(NSString *)novoCelular;
- (id) initWithNomeTelefone:(NSString *)novoNome andTelefone:(NSString *)novoTelefone;
- (id) initWithNomeCelularEmail:(NSString *)novoNome andCelular:(NSString *)novoCelular     andEmail:(NSString *)novoEmail; 
- (id) initWithNomeTelefoneEmail:(NSString *)novoNome andTelefone:(NSString *)novoTelefone andEmail:(NSString *)novoEmail;
- (id) initWithAll:(NSString *)novoNome andSobreNome:(NSString *)novoSobreNome andCelular:(NSString *)novoCelular andTelefone:(NSString *)novoTelefone andEmail:(NSString *)novoEmail;
- (NSString *) nome;
- (void)setNome:(NSString *)novoNome;
- (NSString *) sobreNome;
- (void) setSobreNome : (NSString *) novoSobreNome;
- (NSString *) celular;
- (void) setCelular : (NSString *) novoCelular;
- (NSString *) telefone;
- (void) setTelefone : (NSString *) novoTelefone;
- (NSString *) email;
- (void ) setEmail : (NSString *) novoEmail;
@end

Implementation:

//
//  Registro.m
//  Agenda
//
//  Created by Renan Vicente de Farias on 01/07/14.
//  Copyright (c) 2014 Renan Vicente de Farias. All rights reserved.
//

#import "Registro.h"

@implementation Registro

{
    NSString *nome;
    NSString *sobreNome;
    NSString *celular;
    NSString *telefone;
    NSString *email;
}

- (id)initWithNomeCelular:(NSString *)novoNome andCelular:(NSString *)novoCelular{

   self = [super init];
   [self setNome:novoNome];
   [self setCelular:novoCelular];
   return self;
}

- (id)initWithNomeTelefone:(NSString *)novoNome andTelefone:(NSString *)novoTelefone{

self = [super init];
[self setNome:novoNome];
[self setCelular:novoTelefone];

return self;
}

- (id)initWithNomeCelularEmail:(NSString *)novoNome andCelular:(NSString *)novoCelular andEmail:(NSString *)novoEmail{
self = [super init];
[self setNome:novoNome];
[self setCelular:novoCelular];
[self setEmail:novoEmail];

return self;
}
- (id)initWithNomeTelefoneEmail:(NSString *)novoNome andTelefone:(NSString *)novoTelefone andEmail:(NSString *)novoEmail{
self = [super init];

[self setNome:novoNome];
[self setTelefone:novoTelefone];
[self setEmail:novoEmail];

return self;
}

- (id)initWithAll:(NSString *)novoNome andSobreNome:(NSString *)novoSobreNome andCelular:(NSString *)novoCelular andTelefone:(NSString *)novoTelefone andEmail:(NSString *)novoEmail{

self = [super init];
[self setNome:novoNome];
[self setSobreNome:novoSobreNome];
[self setCelular:novoCelular];
[self setTelefone:novoTelefone];
[self setEmail:novoEmail];

return self;
}

- (NSString *) nome{
    return nome;
}

- (void)setNome:(NSString *)novoNome{
    self.nome = novoNome;
}

- (NSString *)sobreNome{
    return sobreNome;
}

- (void)setSobreNome:(NSString *)novoSobreNome{
    self.sobreNome = novoSobreNome;
}

- (NSString *)celular{
    return celular;
}

- (void) setCelular:(NSString *)novoCelular{
    self.celular = novoCelular;
}

- (NSString *)telefone{
    return telefone;
}

- (void)setTelefone:(NSString *)novoTelefone{
    self.telefone = novoTelefone;
}

- (NSString *)email{
    return email;
}

- (void)setEmail:(NSString *)novoEmail{
    self.email = novoEmail;
}

@end

Please help me out, thank you in advance!

1 answer

2


Renan, the correct way to create the access interface to class attributes is through properties. For each pair of getter/Setter, create a property. Example:

@property(copy, nonatomic) NSString *nome;

This will generate the access methods automatically. You do not need to implement them in . m or declare (NSString *) nome; e (void)setNome:(NSString *)novoNome; no. h.

The property declaration will create an instance variable (i-var) called _nome. Within the class you can read/write the attribute using the property interface, e.g.: self.nome = @"Renam" or accessing the i-var direct, ex: _nome = @"Renam". The difference is that using self.name the setName method will be implicitly called and the other approach will not. The same goes for getter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.