ios - Appending data to a string without losing previous data -


i have on top of program:

@property (strong, nonatomic) nsmutabledata         *data; 

i thought allow me store value every time method runs:

- (void)peripheralmanager:(cbperipheralmanager *)peripheral didreceivewriterequests:(nsarray *)requests {     (cbattrequest *request in requests) {         nsstring *stringvalue = [[nsstring alloc] initwithdata:   [request value] encoding:nsutf8stringencoding];        // have got need?     if ([stringvalue isequaltostring:@"eom"]) {          // have, show data,         [self.textview settext:[[nsstring alloc] initwithdata:self.data encoding:nsutf8stringencoding]];       }      // otherwise, add data on have     [self.data appenddata:[request value]];   } 

}

this method waits write request received , stores value in string. have core bluetooth central sending 3 blocks of data. around data transfer size restriction within bluetooth le. problem can't 3 values stored. trying not store last value add new value end of nsstring or nssdata every time method called. appreciated. thought property @ top allow me either stores last value or nothing @ all. not used ways of objective c yet. thanks.

even doesn't write self.data:

nsstring * result = [[requests valueforkey:@"value"]  componentsjoinedbystring:@""]; nsdata* data = [result datausingencoding:nsutf8stringencoding];   [self.data appenddata:data];  // log nslog(@"%@",self.data); 

you should use nsmutablearray instead of nsstring mutable string.

- (void)peripheralmanager:(cbperipheralmanager *)peripheral didreceivewriterequests:(nsarray *)requests {     nsmutablestring *stringvalue = [[nsmutablestring alloc] init];     (cbattrequest *request in requests) {     [stringvalue appendstring:[[nsstring alloc] initwithdata:[request value] encoding:nsutf8stringencoding]];     // have got need?     if ([stringvalue isequaltostring:@"eom"]) {         // have, show data,     [self.textview settext:[[nsstring alloc] initwithdata:self.data encoding:nsutf8stringencoding]];     }     // otherwise, add data on have     [self.data appenddata:[request value]]; } 

Comments