views:

228

answers:

2

Hello all,

I need some help here so I will do my best to explain. I have worked on this all day and had no success (just learning!)

I have:

NSArray *getValue(NSString *iosearch)
{
    mach_port_t          masterPort;
    CFTypeID             propID = (CFTypeID) NULL;
    unsigned int         bufSize;

    kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
    if (kr != noErr) return nil;

    io_registry_entry_t entry = IORegistryGetRootEntry(masterPort);
    if (entry == MACH_PORT_NULL) return nil;

    CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane,     (CFStringRef) iosearch, nil, kIORegistryIterateRecursively);
    if (!prop) return nil;

    propID = CFGetTypeID(prop);
    if (!(propID == CFDataGetTypeID())) 
    {
        mach_port_deallocate(mach_task_self(), masterPort);
        return nil;
    }

    CFDataRef propData = (CFDataRef) prop;
    if (!propData) return nil;

    bufSize = CFDataGetLength(propData);
    if (!bufSize) return nil;

    NSString *p1 = [[[NSString alloc] initWithBytes:CFDataGetBytePtr(propData)     length:bufSize encoding:1] autorelease];
    mach_port_deallocate(mach_task_self(), masterPort);
    return [p1 componentsSeparatedByString:@"\0"];
}



- (NSString *) serialnumber
{
    NSArray *results = getValue(@"serial-number");
    if (results) return [results objectAtIndex:0];
    return nil;
}

- (NSString *) backlightlevel
{
    NSArray *results = getValue(@"backlight-level");
    if (results) return [results objectAtIndex:0];
    return nil;
}

I have a tableView set up and want to display my array in it so I then have:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {.............
.........................................................
cell.text = [results objectAtIndex:indexPath.row]; // error results undeclared here
    return cell;
}

In my .h file I have:

@interface UIDevice_IOKitExtensionsViewController : UIViewController {
    IBOutlet UITableView *tblSimpleTable;

    NSArray *results;


}

@property (nonatomic, retain) NSArray *results;
- (NSString *) serialnumber;
- (NSString *) backlightlevel;

The problem is I get results undeclared as indicated above. I also can't figure how I could get serialnumber and backlightlevel to display on the click of a button or even at load. My attempts have thrown back errors like error: 'serialnumber' undeclared (first use in this function) and warnings like warning: 'return' with a value, in function returning void. Sorry for the long question!

Many thanks, Stuart

A: 

Where are you declaring results? It sure looks undeclared from the code you've posting. If you're using it as your data source, you should probably set it as an ivar. If you don't know what that means, this is a good place to start if you want to understand iPhone development.

Ian Henry
It is declared in my .h file as NSArray *results
Stumf
When are you setting it? If it's declared as an ivar, it wouldn't give you a "results undeclared" compile-time error, but a "results undefined" or "not set" or something like that error at runtime. Which are you getting?
Ian Henry
It was undeclared, but that is sorted now. When I put the display stage or either of the two NString methods in viewDidLoad or within a buttonClick I get "undeclared" again. I am probably making a basic mistake or mis-understanding a concept.
Stumf
Are you trying to use your methods as variables, or are you actually calling them? `[self serialNumber]` or just `serialNumber`?
Ian Henry
I am not using [self serialNumber], should I be?
Stumf
That is how you call a method in Obj-C (assuming you're calling it form the same class in which it's declared, otherwise replace `self` with the name of the instance). I suggest you take a look at that doc I linked. You can't use C-style method calls (like `serialNumber()`) for non-static methods.
Ian Henry
It would also help if you post the code that's giving you the problem.
Ian Henry
Ok, I posted the code which I am struggling with. I want to put "serial-number" and "backlight-level" through the funtion NSArray *getValue(NSString *iosearch). The two NSStrings returned should be stored in an array and then displayed in my table which I created. I have tried to do this at viewDidLoad, but this did not work. The code runs fine as it is above, but nothing is displayed in my table. When I try and do this I get more errors.
Stumf
I do get a warning at the line if (results) return [results objectAtIndex:0]; and the other one just beneath it [results objectAtIndex:1]; "warning: local declaration of 'results' hides instance variable"
Stumf
+1  A: 

Did you retain the resutls in your two class level methods (serialnumber and backlightlevel)? For example, the serialnumber should be:

- (NSString *) serialnumber
{
  NSArray *results = getValue(@"serial-number");
  if (results) return [[results objectAtIndex:0] autorelease];
  return nil;
}
David.Chu.ca