Swift 4.0
Для async Request-Response вы можете выполнить обработчик завершения пользователем. См. Ниже, я изменил ваше решение с помощью парадигмы обработки завершения.
func getGenres(_ completion: @escaping (NSArray) -> ()) {
let urlPath = "http://creative.coventry.ac.uk/~bookshop/v1.1/index.php/genre/list"
print(urlPath)
guard let url = URL(string: urlPath) else { return }
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
let results = jsonResult["genres"] as! NSArray
print(results)
completion(results)
}
} catch {
//Catch Error here...
}
}
task.resume()
}
Вы можете вызвать эту функцию, как показано ниже. Простой
getGenres { (array) in
// Do operation with your array
}
Ниже вы найдете рабочий код с пояснениями в комментариях.
import java.util.*;
class EncryptDecrypt {
public static void main(String[] args) {
// List to convert ascii characters to cards
ArrayList<String> mylist = new ArrayList<String>();
mylist.add("\uD83C\uDCA1"); mylist.add("\uD83C\uDCB1");
mylist.add("\uD83C\uDCC1"); mylist.add("\uD83C\uDCD1");
mylist.add("\uD83C\uDCA2"); mylist.add("\uD83C\uDCB2");
mylist.add("\uD83C\uDCC2"); mylist.add("\uD83C\uDCD2");
mylist.add("\uD83C\uDCA3"); mylist.add("\uD83C\uDCB3");
mylist.add("\uD83C\uDCC3"); mylist.add("\uD83C\uDCD3");
mylist.add("\uD83C\uDCA4"); mylist.add("\uD83C\uDCB4");
mylist.add("\uD83C\uDCC4"); mylist.add("\uD83C\uDCD4");
mylist.add("\uD83C\uDCA5"); mylist.add("\uD83C\uDCB5");
mylist.add("\uD83C\uDCC5"); mylist.add("\uD83C\uDCD5");
mylist.add("\uD83C\uDCA6"); mylist.add("\uD83C\uDCB6");
mylist.add("\uD83C\uDCC6"); mylist.add("\uD83C\uDCD6");
mylist.add("\uD83C\uDCA7"); mylist.add("\uD83C\uDCB7");
mylist.add("\uD83C\uDCC7"); mylist.add("\uD83C\uDCD7");
mylist.add("\uD83C\uDCA8"); mylist.add("\uD83C\uDCB8");
mylist.add("\uD83C\uDCC8"); mylist.add("\uD83C\uDCD8");
mylist.add("\uD83C\uDCA9"); mylist.add("\uD83C\uDCB9");
mylist.add("\uD83C\uDCC9"); mylist.add("\uD83C\uDCD9");
mylist.add("\uD83C\uDCAA"); mylist.add("\uD83C\uDCBA");
mylist.add("\uD83C\uDCCA"); mylist.add("\uD83C\uDCDA");
mylist.add("\uD83C\uDCAB"); mylist.add("\uD83C\uDCBB");
mylist.add("\uD83C\uDCCB"); mylist.add("\uD83C\uDCDB");
mylist.add("\uD83C\uDCAD"); mylist.add("\uD83C\uDCBD");
mylist.add("\uD83C\uDCCD"); mylist.add("\uD83C\uDCDD");
mylist.add("\uD83C\uDCAE"); mylist.add("\uD83C\uDCBE");
mylist.add("\uD83C\uDCCE"); mylist.add("\uD83C\uDCDE");
// Example string (can be read from file if needed)
String text = "A B C";
// The encrypted string
String encrypted = "";
// Encrypt
// Note: You can use "i" instead of using another variable "index"
for(int i = 0; i < text.length() ; i++){
// Get next character from text (or file)
char symbol = text.charAt(i);
// Convert character to int
int ascii = ((int)symbol);
//if ascii is between A-Z or a-z display a card
if( ((ascii >= (int)'A' && ascii <= (int)'Z'))) {
encrypted += mylist.get(ascii - 65);
} else if(ascii % 2 == 0) { // Using "else if" here makes the code much cleaner
encrypted += "0"; // Here you should append a character "0" instead of the integer 0
} else{
encrypted += "1"; // Here you should append a character "1" instead of the integer 1
}
}
System.out.println("\nYour encrypted message is: \n" + encrypted);
System.out.println("---------------------------------------------\t");
// The decrypted text
String decrypted = "";
// Decrypting this is a little more difficult for multiple reasons.
// 1. The cards are 2 character symbols. Meaning we have to check if the next character is a card or not
// 2. Then, once we know if it is a card or not, we need to take apropriate action
// 3. Inherently we loose some information while encrypting. Therefore we will never get back the exact same string
// Storage to store one character if we encounter a card
String charStore = "";
// Loop through text
for(int i = 0; i < encrypted.length(); i++){
// Get next character
char nextChar = encrypted.charAt(i);
if(charStore.length() == 0) {
// Our character store is empty, so we check what the next character is
if(nextChar == "0".charAt(0)) {
// We found our next character to be a "0". So we convert it to a space
// This is where we lost information. we don't know exactly which character was here before
decrypted += " ";
} else if (nextChar == "1".charAt(0)) {
// We found our next character to be a "1". So we convert it to a !
// This is where we also lost information. we don't know exactly which character was here before
decrypted += "!";
} else {
// The character is neither a "0" nor a "1", so it has to be part of a chard, which is 2 characters in length
// We store the current character in our storage "charStore"
charStore += nextChar;
}
} else {
// Our character store contained at least one character.
// So we can safely assume that together with the next character we have a card symbol
// Concatenate stored and current character
charStore += nextChar;
// Search for the card in the list
// Remember that you subtracted 65 from previously, so we have to add 65 here.
// Also: You forgot the argument inside the "indexOf" in your code
decrypted += (char)(mylist.indexOf(charStore) + 65);
// Reset the character store
charStore = "";
}
}
System.out.println("\nYour decrypted message is: \n" + decrypted);
}
}