Adding an object to a NSArray - xcode

NSString* title = [rescue objectForKey:#"title"];
NSString* name = [rescue objectForKey:#"name"];
NSMutableArray* listBossF = [[NSMutableArray alloc] initWithObjects:#"Hello %# %# I've just arrived"[title, name] ,
#"Hello %# %#everything is fine",
#"Hello %# %# are there any news back at the office?",
#"Hello ( %# %# i arrived at the hotel" ,
#"Hello %# %# I've just arrived at the airport", nil];
Hello im very new to xcode and in fact to programming in general, i've been trying for quite a while to add an object to a specific part of the array (thats why i cant used componentsJoinedByString) but i've had a lot of trouble doing so, the way i'd normally do can anyone tell me what i've been doing wrong? And if i can't add objects to a specific part of an array can anyone possibly give me a suggestion of what i should try? thanks.

You can add an object to a specific part of an array like so:
[array insertObject:#"Hello" atIndex:1]; // After first object in array

Related

How to use "valueforkey"?

I'm trying to do the following - I have an Array in which some strings are stored. These strings shall be used to call an NSArray. An example will clarify what I'm trying to do:
This is the working code that I'm trying to achieve ("briefing0" is of type NSArray):
NSString *path = [docsDir stringByAppendingPathComponent:[briefing0 objectAtIndex:indexPath.row]];
This is the "same" code that I'm trying to use:
int i = 0;
NSString *path = [docsDir stringByAppendingPathComponent:[(NSArray *)[NSString stringWithFormat:#"briefing%d", i] objectAtIndex:indexPath.row]];
Any ideas?
Thanks in advance!
Tom
Assuming that briefing0 is actually a property, then yes, this is possible (and not uncommon) in ObjC via KVC.
int i = 0;
NSString *prop = [NSString stringWithFormat:#"briefing%d", i];
NSArray *array = [self valueForKey:prop];
NSString *value = [array objectAtIndex:indexPath.row];
... etc. ...
-valueForKey: is the piece you're looking for. Note that this will throw an exception if you construct a key that does not exist, and so must be used with extreme care.

Attaching multiple uitextfields to an email

I have multiple UITextFields and I want to attach them to an in app email. I can get one of them appear but not the rest. Here is what I am using.
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
[composer setSubject:#"My Subject"];
[composer setMessageBody:AddNotesTextField.text isHTML:YES];
[self presentModalViewController:composer animated:YES];
[composer release];
Thank you
You got it right already for the body using:
[composer setMessageBody:AddNotesTextField.text isHTML:YES];
So just repeat the procedure with the other fields like so:
[composer setSubject:mySubjectTextField.text];
Or if you are asking how to use the input from multiple text fields into the body of the email, you simply have to use NSString's stringWithFormat:
[composer setMessageBody:[NSString stringWithFormat:#"you can place static %# text in between %# these variables",AddNotesTextField.text,someOtherField.text] isHTML:YES];
In string with format, you escape the default string to add a string using "%#" for every instance where you want to insert some other string. Then after the original string ends you enter the names of the input strings separated by commas. [NSString stringWithFormat:#"%#%#",x,y]

Find out if .png file is a screenshot?

When I get the attributes of a specific file with the following code:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *attributes = [fileManager attributesOfItemAtPath:p error:nil];
Is there an attribute to know if it's a screenshot or not?
Right now I'm using this awkward piece of code:
if ([fileExtension isEqualToString:#"png"]) {
NSMutableArray *separatetFilename;
separatetFilename = [NSMutableArray arrayWithArray:[fileName componentsSeparatedByString:#" "]]; /* screenshots have multiple spaces in their names, I split them up to use the information */
if ([separatetFilename count] == 4) { /* screenshots names have 4 parts when splittet by spaces */
if ([[separatetFilename objectAtIndex:0] isEqualToString:#"Bildschirmfoto"]) { /* Bildschirmfoto = Screenshot in German */
/* Do Something */
}
}
}
With this solution I would have to put in localized Strings for the "Screenshot" string... I searched the documentary but didn't find anything. Is there a "isScreenshot" flag or something I could read?
Your question "Is there a "isScreenshot" flag or something I could read?" and the answer is "YES".
I know of two ways to get the answer:
a) ask the NSFile~Manager
b) ask for metadata.
You already used:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *attributes = [fileManager attributesOfItemAtPath:p error:nil];
If you now have a look at the keys of this dictionary e.g. with
NSLog( #"the keys: %#", [attributes allKeys] );
you will find the key #"NSFileExtendedAttributes*". The value of this key is a dictionary with keys like com.apple.metadata:kMDItemIsScreenCapture or com.apple.metadata:kMDItemScreenCaptureType or .... This is what you asked for. The corresponding values are NSData-objects with a binary property list. Evaluating these plist give something like 1 (it is a screenshot) and window for a window screenshot or selection for a part of the window.
Looking at the metadata can be done with:
MDItemRef item = MDItemCreate( kCFAllocatorDefault, (CFStringRef)p );
If you only need the above mentioned values create a list (an NSArray) with the corresponding keys:
NSArray *ary = [NSArray arrayWithObjects:
#"kMDItemIsScreenCapture", #"kMDItemScreenCaptureType", nil];
NSDictionary *dict =
(NSDictionary *)MDItemCopyAttributes( item, (CFArrayRef)ary );
and test what it returns;
NSLog( #"the metadata are %#", dict );
In this case the returned values are __NSCFBoolean (i.e. NSNumber) and __NSCFString (i.e. NSString) which is a bit simpler to work with than a binary plist.

Building up an array an element at a time

Currently I'm populating an array in one line, e.g.
self.monthMonths = [[NSArray alloc] initWithObjects:#"January", #"February", #"March", #"April", #"May", #"June",#"July",#"August",#"September",#"October",#"November",#"December", nil];
What is the syntax to add these elements one at a time as I want to pull the data from a database. I'm using the months of the year as an example.
while([results next]) {
NSString *months = [results stringForColumn:#"month"];
self.month = [[NSArray alloc] initWithObjects:#"month",nil];
//[NSArray
NSLog(#"Month: %#",month);
}
Create an NSMutableArray and add the objects to it one by one with addObject
You need to use NSMutableArray instead, and call -addObject:

componentsJoinedByString gives me EXC_BAD_ACCESS

I have an NSMutableArray i am trying to convert into a string.
Declaring my NSMutableArray...
NSMutableArray *listData;
And later inside a method...
NSString *foo = [listData componentsJoinedByString:#"|"];
NSLog(#"%#",foo);
It seems no matter what i try i keep getting EXC_BAD_ACCESS.
To make sure each element in my array was an NSString i also tried this...
NSMutableArray *mArray = [[NSMutableArray alloc] init];
for (id ln in listData) {
NSString *boo = [NSString stringWithFormat: #"%#",ln];
[mArray addObject:boo];
}
NSString *foo = [mArray componentsJoinedByString:#"|"];
NSLog(#"%#",foo);
I can manipulate my NSMutableArray by adding/deleting objects in the same method or other methods inside my class. But when i try "componentsJoinedByString" the error pops up. Does anyone have any advice or another way i can combine this array into a single NSString?
In the code you've given, there will never be an NSMutableArray for listData. At some point in your code, you'll need to create one, and presumably populate it.
Edit
Okay, so you may get into memory management problems here, so let's be a bit clearer:
You're synthesizing getters and setters for the instance variable, so it's good practice to use those to access it, they'll take care of retain and releasing appropriately.
To set listData you can simply use
self.listData = [listManage getList:[[NSUserDefaults standardUserDefaults] stringForKey:#"list_name"] list:#"LIST"];
or
[self setListData:[listManage getList:[[NSUserDefaults standardUserDefaults] stringForKey:#"list_name"] list:#"LIST"]];
if you prefer.

Resources