Co oznacza znak zapytania i dwukropek (?: operator trójdzielny) oznacza w objective-c?

Co oznacza ta linijka kodu?

label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;

The ? and : confuse me.

Author: Forge, 2010-04-07

13 answers

Jest to operator trójkowy C (Objective-C jest supersetem C):

label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;

Jest semantycznie równoważne

if(inPseudoEditMode) {
 label.frame = kLabelIndentedRect;
} else {
 label.frame = kLabelRect;
}

Trójnik bez pierwszego elementu (np. variable ?: anotherVariable) oznacza to samo co (valOrVar != 0) ? valOrVar : anotherValOrVar

 397
Author: Barry Wark,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-03 15:47:37

Jest operatorem trójdzielnym lub warunkowym. Jego podstawową formą jest:

condition ? valueIfTrue : valueIfFalse

Gdzie wartości będą oceniane tylko wtedy, gdy zostaną wybrane.

 167
Author: Sean,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-26 02:08:48

Bazując na doskonałym wyjaśnieniu Barry ' ego Warka...

Tak ważne w operatorze trójdzielnym jest to, że może być używany w miejscach, w których if-else nie może. ie: wewnątrz warunku lub parametru metody.

[NSString stringWithFormat: @"Status: %@", (statusBool ? @"Approved" : @"Rejected")]

...co jest bardzo przydatne dla stałych preprocesora:

// in your pch file...
#define statusString (statusBool ? @"Approved" : @"Rejected")

// in your m file...
[NSString stringWithFormat: @"Status: %@", statusString]

Pozwala to uniknąć konieczności używania i zwalniania zmiennych lokalnych we wzorcach if-else. FTW!

 35
Author: Richard Bronosky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-06 16:09:25

Po prostu logika byłaby

(condition) ? {code for YES} : {code for NO}

 34
Author: Varun Goyal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-23 17:13:45

To zwykły Operator trójkowy . Jeśli część przed znakiem zapytania jest prawdziwa, ocenia i zwraca część przed dwukropkiem, w przeciwnym razie ocenia i zwraca część po dwukropku.

a?b:c

Jest jak

if(a)
    b;
else
    c;
 13
Author: Brian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-07 19:49:31

Jest to część C, więc nie jest to Objective-C specyficzne. Oto tłumaczenie na if oświadczenie:

if (inPseudoEditMode)
    label.frame = kLabelIndentedRec;
else
    label.frame = kLabelRect;
 4
Author: Dietrich Epp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-07 19:46:15

To tylko krótka forma pisania Oświadczenia if-then-else. Oznacza to to samo co następujący kod:

if(inPseudoEditMode)
  label.frame = kLabelIndentedRect;
else
  label.frame = kLabelRect;
 4
Author: Claus Broch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-27 20:49:00

Jest operatorem trójdzielnym, podobnie jak instrukcja if / else.

if(a > b) {
what to do;
}
else {
what to do;
}

W operatorze trójdzielnym jest tak: warunek ? co zrobić, jeśli warunek jest prawdziwy : co zrobić, jeśli jest fałszywy;

(a > b) ? what to do if true : what to do if false;
 1
Author: cdhw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-06 21:03:26

Właśnie dowiedziałem się czegoś nowego o operatorze trójdzielnym. Krótka forma, która pomija środkową operandę, jest naprawdę elegancka i jest jednym z wielu powodów, dla których C pozostaje aktualna. Dla twojej wiadomości, po raz pierwszy skupiłem się na tym w kontekście procedury zaimplementowanej w C#, która również obsługuje Operator trójkowy. Ponieważ operator trójdzielny jest w języku C, można przypuszczać, że w innych językach byłby on zasadniczo jego rozszerzeniami (np. Objective-C, C#).

 1
Author: David A. Gray,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-19 08:30:01

Jak wszyscy o tym mówili, jest to sposób przedstawiania operatora warunkowego

if (condition){ 
    true 
} 
else {
    false
}

Używając operatora trójdzielnego (condition)? true:false Aby dodać dodatkowe informacje, w języku swift mamy nowy sposób przedstawiania ich za pomocą ??.

let imageObject: UIImage = (UIImage(named: "ImageName")) ?? (initialOfUsername.capitalizedString).imageFromString

Który jest podobny do

int a = 6, c= 5;
if (a > c) 
{ 
 a is greater
} else {
 c is greater
}

Jest równoważne

if (a>c)?a:c ==> jest równe if (a>c)?:c

Zamiast ?: możemy użyć ?? jest swift.

 1
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-23 08:52:50
int padding = ([[UIScreen mainScreen] bounds].size.height <= 480) ? 15 : 55;

Oznacza

int padding; 
if ([[UIScreen mainScreen] bounds].size.height <= 480)
  padding = 15;
else
  padding = 55; 
 1
Author: Abo3atef,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-07 06:27:33

Przykład operatora trójdzielnego.Jeżeli wartość isFemale zmienna boolean jest tak, Drukuj "płeć jest żeńska" w przeciwnym razie " płeć jest Mężczyzna "

? means = execute the codes before the : if the condition is true. 
: means = execute the codes after the : if the condition is false.

Objective-C

BOOL isFemale = YES;
NSString *valueToPrint = (isFemale == YES) ? @"GENDER IS FEMALE" : @"GENDER IS MALE";
NSLog(valueToPrint); //Result will be "GENDER IS FEMALE" because the value of isFemale was set to YES.

Dla Swift

let isFemale = false
let valueToPrint:String = (isFemale == true) ? "GENDER IS FEMALE" : "GENDER IS MALE"
print(valueToPrint) //Result will be  "GENDER IS MALE" because the isFemale value was set to false.
 1
Author: handiansom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-21 10:06:16

Ciekawostka, w objective-c jeśli chcesz sprawdzić null / nil Na przykład:

-(NSString*) getSomeStringSafeCheck
{
    NSString *string = [self getSomeString];
    if(string != nil){
        return String;
    }
    return @"";
}

Szybki sposób to:

-(NSString*) getSomeStringSafeCheck
{
    return [self getSomeString] != nil ? [self getSomeString] : @"";
}

Następnie możesz zaktualizować go w najprostszy sposób:

-(NSString*) getSomeStringSafeCheck
{
    return [self getSomeString]?: @"";
}

Ponieważ w Objective-C:

  1. jeśli obiekt jest zerowy, zwróci false jako wartość logiczna;
  2. drugi parametr operatora trójdzielnego może być pusty, ponieważ zwróci wynik po lewej stronie '?'

Więc powiedzmy, że piszesz:

[self getSomeString] != nil?: @"";

Drugi parametr zwraca wartość logiczna, więc wyjątek jest wyrzucany.

 1
Author: Jun Jie Gan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-26 01:57:01