1

This is my NSObject code;

Task.h

#import <Foundation/Foundation.h>

@interface Task : NSObject

@property (nonatomic,strong) NSString *name;
@property (nonatomic,assign) BOOL done;

-(id)initWithName:(NSString *)name done:(BOOL)done;

@end

Task.m

#import "Task.h"

@implementation Task

@synthesize name = _name;
@synthesize done = _done;

-(id)initWithName:(NSString *)name done:(BOOL)done {
    self = [super init];

    if (self) {
        self.name = name;
        self.done = done;
    }
    return self;
}

This is my send mail code

Task *task = [[Task alloc]init];
        MFMailComposeViewController *sendmail = [[MFMailComposeViewController alloc]init];
        [sendmail setMailComposeDelegate:self];
        NSString *message = [_tasks addObject:task]; // Error is here.
        [sendmail setMessageBody:message isHTML:NO];
        [sendmail setSubject:@"Test"];
        [self presentViewController:sendmail animated:YES completion:nil];

I don't know, How to do it. I just want to send the list with mail. Where is my mistake? And How can I fix this?


Tasklistviewcontroller.m

@synthesize tasks = _tasks;

I am transferring from the tasks table view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *NotDoneCellIdentifier = @"NotDoneTaskCell";
    static NSString *DoneCellIdentifier = @"DoneTaskCell";

    Task *currentTask = [self.tasks objectAtIndex:indexPath.row];

    NSString *cellIdentifier = currentTask.done ? DoneCellIdentifier : NotDoneCellIdentifier;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = currentTask.name;

    return cell;
}
Till
  • 27,559
  • 13
  • 88
  • 122
Salieh
  • 693
  • 2
  • 7
  • 12
  • Your source is incomplete. What is `_tasks`? – ott-- Mar 31 '13 at 21:13
  • 1
    What does this have to do with a table view? – rmaddy Mar 31 '13 at 21:13
  • There's still some code missing about what type `tasks` is. I guess NSArray or NSMutableArray. Anyway, `[_tasks addObject:task];` will not return a NSString. And after you've done `@synthesize tasks = _tasks;` you shouldn't use `self.tasks` anymore, simply use `_tasks = ...`. Same for _name and _done. – ott-- Mar 31 '13 at 22:49
  • But setMessageBody only accepts an NSString, so i will need to convert the NSArray to that. And i dont know how i fix this. – Salieh Mar 31 '13 at 22:55

1 Answers1

0

I don't know what is exactly wrong in your code since you don't supply an error and I'm not that proficient yet in Objective-C.

I suspect it is because you reference to "_tasks" which I don't see other code for creating that Class.

NSString *message = [_tasks addObject:task];

Another problem is that you're using task as input object for the array, but it probably doesn't contains something.

You probably wan't something like this:

 Task *task = [[Task alloc] initWithName:@"Task 1"];
 NSString *message = [[NSString alloc] initWithFormat:@"Task name is %@", task.name];

Also I'm guessing you haven't posted your complete code.

You also forgot to include the right framework for mailing in-app in your header file:

#import <MessageUI/MFMailComposeViewController.h>

Don't forget to also add the framework it to your project!

By the way, you can remove the two lines with synthesize, the compiler does this automatically these days. Nice isn't it?

edwardmp
  • 6,339
  • 5
  • 50
  • 77
  • I have more than one task. Also we have one error, initWithFormat line. No visible @interface for 'Task' declares the selector 'initWithName:' I change this code like: Task *task = [[Task alloc] initWithName:@"Task 1" done:NO]; then my mail messagebody is Task name is Task 1. – Salieh Mar 31 '13 at 22:03
  • If you have more than 1, you will need an array, but `setMessageBody` only accepts an NSString, so you will need to convert the NSArray to that. And yes, I forget the second done:NO but you figured that out yourself ;). But the Mail View is opening correctly already? – edwardmp Mar 31 '13 at 22:37
  • Yes mail view opening corretly. But How i convert the NSArray? I dont know any idea. – Salieh Mar 31 '13 at 22:48
  • You're missing quite some code to make this work.. Except that you will need to convert, you also need to retrieve the data form the UITableView. It's late here, maybe I'll try tomorrow! – edwardmp Mar 31 '13 at 23:07
  • This might help you get started for convertering the NSArray to NSString: http://stackoverflow.com/questions/1828665/convert-nsarray-to-nsstring-in-objective-c – edwardmp Mar 31 '13 at 23:08
  • I have no idea about this topic. I'm waiting for someone to help me. thank you a lot. – Salieh Mar 31 '13 at 23:47
  • Please contact me through my profile/website, then I'll try to help further. – edwardmp Apr 02 '13 at 00:59
  • I figured out the problem. Thank you for that. Final status codes: http://pastie.org/7271927 – Salieh Apr 02 '13 at 01:34
  • Good for you, hope you could use some of my ideas. – edwardmp Apr 03 '13 at 01:42